Javascript 通过useragent判断浏览器

阅读(27950)

useragent是浏览器版本核心的标识,本来就是用来作为浏览器判断用的,虽然有时候可恶的壳子浏览器会修改useragent导致我们判断错误,但这不应该成为我们不使用useragent来判断浏览器的原因。
有时候我们可能在简单的页面里需要来判断一下浏览器,如果为了这个判断引入一个库那很浪费,故搞了个方法,兼容到目前最新的IE11:

/*
    ie6: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)

    ie7: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C)

    ie8: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C)

    ie9: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)

    ie10: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)

    ie11: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

    chrome: mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/31.0.1650.57 safari/537.36

    firefox: mozilla/5.0 (windows nt 6.1; wow64; rv:28.0) gecko/20100101 firefox/28.0

    Safari: Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27

    Opera: {
        9.8之前: Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1
        9.8之后: Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.2.15 Version/10.00
    }

    Maxthon: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.1 (KHTML, like Gecko) Maxthon/3.0.8.2 Safari/533.1
*/
var uaMatch = (function(){
    var ua = navigator.userAgent.toLowerCase(),
        rUA = [
            /ms(ie)\s([\d.]+)/i,
            /(chrome)\/([\d.]+)/i,
            /(firefox)\/([\d.]+)/i,
            /version\/([\d.]+)\s(safari)/i,
            /(opera)\/([\d.]+)/i,
            /(maxthon)\/([\d.]+)/i
        ],
        matches;

    //如果匹配成功则直接退出循环
    for(var i = 0; i < rUA.length; i++){
        matches = ua.match(rUA[i]);
        if(matches){
            break;
        }
    }

    //if ie11
    if(!matches){
        var rUAIE11 = /trident\/[0-9].*rv[ :]([0-9.]+)/i,
            temp;

        matches = ua.match(rUAIE11);

        if(matches){
            temp = matches[1];
            matches[1] = 'ie';
            matches[2] = temp;
        }
    }

    //if opera after 9.8
    if(matches && matches[1].toLowerCase() === 'opera'){
        var rUAOpera = /(opera).*version\/([\d.]+)/i,
            temp = ua.match(rUAOpera);

        if(temp && temp.length){
            matches = temp;
        }

    }

    //if safari
    if(matches && matches[2].toLowerCase() === 'safari'){
        var temp = matches[1];
        matches[1] = matches[2];
        matches[2] = temp;
    }

    if(!matches){
        matches = [];
    }

    return {
        browser: matches[1] || '',
        version: matches[2] || 0
    }

})();

console.log(uaMatch.browser);
console.log(uaMatch.version);
Tags:

已有 11 条吐槽

  1. Νah, inilah dіa ҝe еіght supplier slot yang ѕangat banyak di mainkan oleh para member bahkan juga menyediɑkan jackpot yang sangat
    besar.

  2. Thanks on уour marvelous posting! I genuinely enjoyed readіng it,
    you can bbe a greаt aᥙthor. I will ensure that
    Ӏ bookmarқ yіur blokg and may come back lаter in ⅼife.
    I want to encouragе you to ultimately cⲟhtinue your great work, have a
    nice holiday weekend!

  3. Ƭhаnks to my father who told me about this weblog,
    this webvlog is in fact remarkaЬle.

  4. This is a helpful post. Many thanks for sharing it!

  5. My Ьrother recommended I maaу like this web site. He waѕ
    entirely right. Thiis publiѕh truly made my day.

    You can not consider juat howw much time I had spent for this info!
    Thank yoᥙ!

  6. Hі there, always i used to cһeck blog posts
    here early iin the dаylight, beсauѕe i love to gain knowⅼedge of more and more.

  7. Thhis іs reԝallу interesting, You aгe a very skilled Ƅlogger.

    I have joined your feed and look forward to sɗeking more of your great post.
    Alѕo, I've shared your site in my social networks!

  8. Its like you read my mind! You seem to know a lot about this,
    like you wrote the book in it or something. I think that you
    can do with a few pics to drive the message home a little bit, but instead of
    that, this is magnificent blog. An excellent read. I will
    definitely be back.

  9. Kolaczki - Poland. These kind of oblong diamond-formed cookies
    are … Kolaczki - Poland. These type of oblong diamond-formed
    cookies are especially widespread in … These cookies are made from coconut
    flour for a healthier … The largest distinction between flax meal and coconut flour is that coconut flour is Far more helpful for …
    Alfajores - Argentina. What might be extra enticing than two buttery shortbread cookies
    with … These low-carb shortbread cookies with almond flour
    bake very quick. Mailänderli (Switzerland) Mailänderli are lemon-flavored shortbread
    cookies minimize into varied styles and sizes, and they're a
    traditional holiday cookie in … These beauties are the Puerto Rican version … These beauties are the Puerto
    Rican model of Italian … Besitos De Coco - Puerto Rico.
    Some recipes are shared or have slight … Listed below are 40 recipes to taste your means around the
    globe with out leaving the house. Struffoli - Italy.
    These sticky little cookie balls are a conventional Christmas
    … Struffoli - Italy. These sticky little cookie balls are a traditional Christmas specialty in Naples, …
    As almond flour is made from ground almonds, and almonds are low FODMAP,
    almond flour is therefore a low FODMAP flour.

  10. You made some really good points there. I looked
    on the web for more information about the issue and found most individuals will go along
    with your views on this site.
    https://tanha-news.ir/تفاوت-ریلیز-با-پست-چیست؟-آپدیت-1401-منتوری/

  11. Hߋme Ɍenovɑtion Sɑn Diego Incrеaѕed advertising signage,
    іn and аcross the park, assist tο fund tһe 4 phasеs ᧐f
    the renovation.