JS to determine browser type and language

Source: Internet
Author: User

1, check whether it is mobile, ipad, iphone, QQ, etc.

<script type= "Text/javascript" >//Judging the access terminal        varBrowser={versions:function(){            varU =navigator.useragentreturn{trident:u.indexof (' Trident ') >-1,//IE kernelPresto:u.indexof (' presto ') >-1,//Opera KernelWebkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernelGecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = =-1,//Firefox kernelMobile:!! U.match (/applewebkit.*mobile.*/),//whether it is a mobile terminalIos:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//iOS terminalAndroid:u.indexof (' Android ') >-1 | | U.indexof (' Adr ') >-1,//Android TerminalIphone:u.indexof (' IPhone ') >-1,//whether it's an iphone or Qqhd browserIpad:u.indexof (' IPad ') >-1,//whether ipadWebapp:u.indexof (' Safari ') = =-1,//whether the web should program, no head with the bottomWeixin:u.indexof (' Micromessenger ') >-1,//isQq:u.match (/\sqq/i) = = "QQ"//whether QQ                }; }(),          //languageLanguage: (Navigator.browserlanguage | |navigator.language). toLowerCase ();</script>

How to use:

//determine if IE kernel        if(browser.versions.trident) {alert ("is IE browser"); }         //determine if the kernel is WebKit        if(Browser.versions.webKit) {Alert ("is WebKit kernel browser"); }         //determine if the mobile side        if(browser.versions.mobile| | browser.versions.android| | Browser.versions.ios) {alert ("mobile"); }        //determine if a browser is built in        if(browser.versions.weixin) {alert ("Built-in browser")}        //determine if QQ built-in browser        if(BROWSER.VERSIONS.QQ) {alert ("QQ built-in browser")}        //Judgment Language        if(Browser.language.indexOf (' zh ') >-1) {alert (Chinese); }Else if(Browser.language.indexOf (' en ') >-1) {alert (English)        }Else{alert (' Other languages ')        }

2, only judge, can be abbreviated like this

    if (Navigator.userAgent.indexOf (' Micromessenger ') >-1) {               alert (' inside ');        } Else {            alert (' outside ')        }    

3. Only the browser language is judged

var language = (Navigator.browserlanguage | | navigator.language). toLowerCase (); if (Language.indexof (' zh ') >-1) {    alert (' Chinese ');} Else if (Language.indexof (' en ') >-1) {    alert (' English ')}else{    alert (' Other languages ')}

4, Judge iphone|ipad|ipod|ios| Android Client | PC-side, jump to different pages separately

if // Judging Iphone|ipad|ipod|ios  Window.location.href = "iphone.html";} Else if (/(Android)/i.test (navigator.useragent)) {  // judging Android  window.location.href = "android.html";} Else // pc  Window.location.href = "pc.html";};

5. Determine if it is iOS or Android client

if (Navigator.userAgent.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/){    alert ("IOS");} Else if (Navigator.userAgent.indexOf (' Android ') >-1 | | navigator.userAgent.indexOf (' ADR ') >-1) {    alert (" Android ")}

6, judge the PC or mobile end

functionIsPC () {varUseragentinfo =navigator.userAgent.toLowerCase (); varAgents = ["Android", "iphone", "SymbianOS", "Windows Phone", "ipad", "ipod"];  for(varv = 0; v < agents.length; v++) {        if(Useragentinfo.indexof (agents[v]) >= 0) {alert ("Mobile Side"); return false; }    }    return true;} 

7. Determine the version of IE browser

functionieversion () {varuseragent = navigator.useragent;//get the useragent string for the browser    varIsie = Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1;//determine if the browser is ie<11    varIsedge = Useragent.indexof ("Edge") >-1 &&!isie;//determine if IE's Edge browser    varisIE11 = Useragent.indexof (' Trident ') >-1 && useragent.indexof ("rv:11.0") > 1; if(Isie) {varReie =NewRegExp ("MSIE (\\d+\\.\\d+);");        Reie.test (useragent); varFieversion = parsefloat (regexp["$"]); if(Fieversion = = 7) {            return7; } Else if(Fieversion = = 8) {            return8; } Else if(Fieversion = = 9) {            return9; } Else if(Fieversion = = 10) {            return10; } Else {            return6;//IE version <=6        }       } Else if(Isedge) {return' Edge ';//Edge}Else if(isIE11) {return11;//IE11}Else{        return-1;//Not IE browser    }}

JS to determine browser type and language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.