Javascript 通过useragent判断浏览器

阅读(29644)

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:

已有 22 条吐槽

  1. Test out your current workplace supplies. First issues first, test out all of your workplace provides, such as
    pens, to make sure they work. Earlier than you get began sorting by your recordsdata, go out on just a
    little buying spree and buy: new file folders, a filing cabinet or filing packing containers and one thing to make
    labels with. Buy new file folders and a filing cabinet.
    Buy a pc desk that has sufficient storage space in your arduous disk drive and keyboard.
    Buy new workplace supplies. What must you do along with your electronic wires in your house
    office? Electricity can kill you, and anything in your house that may pose a serious risk to your safety ought to be
    treated with the respect it deserves. Water guns of all kinds can enable you bring on the
    splashes with out the pain, and with out the
    necessity to fill and tie dozens of balloons.

    This may also help you establish where your recordsdata must be stored.

    It's also possible to look into the companies of a
    professional shredding company. Clearly, a blimplike
    object floating at 1,000 ft (305 meters) could obtain fairly a beating from
    the weather, but the company estimates the MARS should final a
    minimum of 15 years earlier than requiring upkeep.

  2. It then takes up the Brombach coming from the
    Großer Brombachsee and passes several mills
    and the villages of Mühlstetten and Niedermauk, two districts
    of the municipality of Röttenbach in the Roth district.
    After main hydraulic engineering measures, the Swabian Rezat now, along with different bodies of water,
    conveys water from the rain-wealthy south
    of Bavaria to the rain-poor north of Bavaria. Higher reaches of unknown identify above the large pond and approx.
    Frequency / consistency and strength of the wind (required is a
    wind energy from approx. Water, measured right down to the extent of
    the unique stream valley, is up to 13.Four meters deep.

    Additionally to the tip of the mountain spur There are two extra moats, certainly one of
    which is still six meters deep. A little later, the Swabian Rezat is fed by the outflow of a sewage treatment plant.
    Alongside the federal highway 466, which he crosses two times after the
    forest leak on the north -east course on the north -japanese place of
    Markt Gnotzheim, he reaches it there and runs briefly
    below its western tip. Lastly, the Swabian Rezat unites between the namesake
    of the municipality of Georgensgmünd on the left and its district
    of Petersgmünd on the right at 341.7 m excessive with the southwest, some
    longer and more receptive Franconian Rezat for the oblique Most important
    inflow Rednitz.

  3. on the Altmühlsee and the great Brombachsee additionally found their means into passenger transport.
    However, the large branches, such because the Altmühl gas supervisor,
    were talked about . The castle backyard in the center of Erlangen will probably
    be converted into a large"ballroom" below the open sky within the course of the preparations.
    In December 2003, the holiday park North Sea Tropen Parc was bought from the Eurohypo and below
    the title Butjadinger coast ( right now: Park Nordsekae ) in the chain. Park Zandvoort inthe Netherlands and
    Parc Sandur additionally switched to Sunparks. Later, Center
    Parcs began a new attempt to supply a number of of the simpler parks
    below the Sunparks model. In January 2009,
    Sunparks began in Europe as a low- cost brand. 9 September
    2019, accessed January 19, 2020 ( English ). The Irish Occasions, December 15, 2017, accessed
    May 11, 2022 ( English ). In December 2003, Mid Ocean agreed
    to sell its UK resorts to Arbor Ltd. Following the sale, 5 of Gran Dorado Resorts &# 39, six remaining parks
    were renamed Sea Spirit from Center Parcs on the coast or Free Life
    from Center Parcs inland in 2004. In June 2015, Blackstone agreed to promote the
    company to Canada- primarily based Brookfield Properties Corporation for £2.
    4 billion. Rothenburg ob der Tauber Ansbach On city wall, beneath
    the Kobolzeller Tor, in Rothenburg.

  4. If you’re going to test on one in every of the Chinese language maker’s units - and you need to - it needs to be this one as a result
    of it currently holds steady as a high smartphone in 10 countries.
    One of Utzon's sketches from 1962 indicates that "Chinese language houses and temples" inspired him to build the opera home.
    Danish architect Jorn Utzon designed the
    Sydney Opera House of Sydney, Australia. Today, there appears to be a
    trendy café on every road nook, even in the suburbs, and younger celebrities like Mary-Kate and Ashley Olsen are
    often photographed with "venti" Starbucks cups in hand, an accessory practically as fashionable as the newest designer pooch
    or Fendi handbag. In a examine revealed in Horological Journal in 2008, a researcher
    from the Nationwide Institute of Requirements and Know-how used subtle scientific tools to test the performance
    of 4 cheap watches, including a counterfeit Rolex bought from a road vendor for $15.

  5. Max Max

    Test your knowledge in our asbestos quiz. Asbestos isn't one thing
    you need to know too little about, particularly when you've got an older dwelling or work in an older
    building, but is it as unhealthy as its rep? While most of Texas’ rivers are clean and slow, some of the state’s huge array of rivers do have intermediate rapids to plunge down on a kayak.

    Whereas many consider that pore strips are the top all, be all of blackhead maintenance, consultants say that's simply not true.
    Consultants say you can begin using it as early as 21 years outdated, and it is also advisable
    to NOT wait till you already see indicators of aging.
    Whereas there isn't any precise time that one ought to begin using
    anti-aging cream, the earlier you use it, the better likelihood you have to staving off wrinkles and aging skin. Smoking makes your pores and skin look older,
    contributing to wrinkles and damages collagen and elastin largely because smoking narrows
    the tiny blood vessels in the out layers of the skin and decreases blood circulate.
    There are numerous factors that go into labeling
    pores and skin care merchandise. Checking ingredient labels and searching for
    things that you care about in products remains to be key.

  6. The last form of the shoes is usually far too slender and most models are far too pointed at the front.
    The soles of the ft and palms of the hands are covered with a very giant
    variety of eccrine sweat glands, often round 500 per cm2.
    The tendon system in the soles of your toes can also be specifically pressured while you stroll
    barefoot. It's because it tries to compensate for the raised angle
    by leaning again barely, which has an impact in your posture and gait.
    It is commonly a so-called fatigue fracture, which may be attributable
    to extreme sports training, excessive pressure or osteoporosis.
    In my expertise, the soreness may even be felt as far
    down as a result of the decrease again. Similar to walking barefoot, there
    isn't a difference in top from the heel to the ball of the foot
    in our barefoot sneakers. The fact that shoes may adapt to the toes is an aspect
    that may be specifically mixed with a product in the current thanks to the respective design. They
    are additionally an excellent selection if you'll like to offer youngsters's ft better safety.
    Later on, you may sort out terrain that calls for extra out of your
    feet.

  7. barefoot sneakers are characterized by the very flat sole.

    In contrast to ordinary sneakers, the idea of ​​barefoot sneakers is based in the unique
    type of working, particularly the barefoot working. I used them as operating footwear for a few years.
    I need to confess that I had at all times refrained from barefoot
    shoes. The second variant of the barefoot shoes are light running sneakers.
    The second necessary feature of minimal footwear is that the heel and the only
    real are at the same peak. From the third week to the top
    of the fourth week, the runner begins to complete about 10 percent of the standard operating route with minimal shoes.
    In this way, you gently prepare your toes to wear minimal sneakers.

    Since there are no supporting components, the muscles of the legs and
    feet are closely used, stimulated and strengthened and the joints are stabilized.

    The construction works as follows: The sole has little or no cushioning, so it is not stiff and can adapt well to the
    toes and the floor. The tracker is therefore particularly suitable for cooler seasons,
    but in addition for those who want to be
    out and about for a bit of longer in the rain. Your feet are very sensitive and you often not feel your
    toes well - this may lead to injuries.

  8. Hi r/en, I have been desirous about switching to barefoot shoes for some time
    now. We also love carrying Wildling's barefoot shoes in the winter months.
    Can youngsters additionally wear barefoot sneakers? Are there waterproof barefoot footwear for men? Many barefoot shoes focus on insulating
    each particular person toe and also have a thick "excessive-tech" sole.
    The very first thing it is best to consider when making your first buy is the thickness and profile of
    the sole. 2. Select special floors for your first barefoot steps.
    You should not run greater than half a kilometer
    on your first run, as your foot muscles will tire very quickly.
    High FEELS are my first barefoot footwear. Opinions on barefoot shoes for jogging are divided.
    Only when you're feeling one hundred p.c snug operating barefoot and with none discomfort can you
    start operating more often in barefoot footwear. Bürkner: "Inside, the feet are as versatile as naked feet. The toes are usually not restricted laterally, the heel touches the bottom without a heel and cushioning. " A examine by the Bundeswehr
    University in Munich confirmed that athletes strengthen their foot muscles 30
    p.c more when operating with barefoot shoes than runners in regular running footwear.

  9. Thhe Fitbit Zip is a small gadget that's straightforward to hold as a result
    of its ability to clip on to your clothning simply.

  10. Clear. There is no better way to scrub the kitchen, bathroom or work surfaces than with this sponge
    cloth. Can be used with water, cleaning soap and all forms of family cleaners and may
    be washed within the washing machine and dishwasher at up to 90°C.
    As a result of it may be machine washed and reused many instances,
    this durable cloth is especially sustainable. The sponge cloth might be washed several instances
    in the washing machine at 95°C. The Spontex sponge cloth is established as a premium branded
    item in the commerce and is used in family cleaning and likewise in the
    skilled cleaning sector. The Spontex sponge cloth is obtainable in numerous colors, qualities and
    thicknesses. Spontex sponge cloth has been produced utilizing a proven process for a lot
    of decades - utilizing regenerated cellulose fibers and cotton.
    A glass bottle has been accessible since 2015. Manufacturing has been carried out by Freudenberg &
    Co. since 1990. ↑ Working at Freudenberg Dwelling and Cleaning Options.
    ↑ Duden editorial group: Wettex®, this or that. However this drug was not always a part
    of their culture: it was solely launched by
    the colonial powers, and the Akha discovered it for themselves.

  11. Thiѕ sitе was... howw doo I say it? Relevant!! Finally I've found something
    which helped me. Thanks a lot!

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

  13. 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!

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

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

  16. 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ᥙ!

  17. 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.

  18. 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!

  19. 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.

  20. 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.

  21. 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-منتوری/

  22. 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.