JS to determine if the client is iOS or Android

Source: Internet
Author: User

* * First: By judging the browser's useragent, use the regular to determine whether it is an iOS and android client. The code is as follows: *
<script type="text/javascript">
    var u = navigator.userAgent;
    var isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Adr‘) > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    alert(‘是否是Android:‘+isAndroid);
    alert(‘是否是iOS:‘+isiOS);
</script>


* * The second type: check whether the mobile side (Mobile), ipad, iphone, QQ, etc. **
<script type= "Text/javascript" >
Judging the access terminal
var browser={
Versions:function () {
var u = navigator.useragent,
App = Navigator.appversion;
return {
Trident:u.indexof (' Trident ') >-1,//ie core
Presto:u.indexof (' presto ') >-1,//opera core
Webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel
Gecko:u.indexof (' Gecko ') >-1 && u.indexof (' khtml ') = = -1,//Firefox kernel
Mobile:!! U.match (/applewebkit.*mobile.*/),//Whether it is a mobile terminal
Ios:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal
Android:u.indexof (' Android ') >-1 | | U.indexof (' Adr ') >-1,//android terminal
Iphone:u.indexof (' iphone ') >-1,//whether for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether ipad
Webapp:u.indexof (' Safari ') = =-1,//whether the Web should be program, no head with bottom
Weixin:u.indexof (' Micromessenger ') >-1,//whether (added 2015-01-22)
Qq:u.match (/\sqq/i) = = "QQ"//whether QQ
};
}(),
Language: (Navigator.browserlanguage | | navigator.language). toLowerCase ()
}
</script>


How to use:

Determine if IE kernel
if (browser.versions.trident) {alert ("is IE");}
Determine if the kernel is WebKit
if (browser.versions.webKit) {alert ("is WebKit");}
Determine if the mobile side
if (browser.versions.mobile| | browser.versions.android| | Browser.versions.ios) {alert ("mobile Side");}

Detecting Browser language

Currentlang = Navigator.language; Determine the language used by other browsers except IE
if (!currentlang) {//To determine the language used by IE browser
Currentlang = Navigator.browserlanguage;
}
alert (Currentlang);

* * The third type: judging iphone|ipad|ipod|ios| Android client, from http://www.fufuok.com/js-iphone-android.html**
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  //判断iPhone|iPad|iPod|iOS
    //alert(navigator.userAgent);  
    window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) { //判断Android
    //alert(navigator.userAgent);
    window.location.href ="Android.html";
} else { //pc
    window.location.href ="pc.html";
};


* * Fourth: Judging the PC or the mobile side * *
                  "SymbianOS", "Windows Phone", 
                 "ipad", "ipod");
    var ly=document.referrer; //Returns the URL of the page that contains the hyperlink that navigates to the current page

    for (var v = 0; v < agents.length; v++) {
        if (Useragentinfo.indexof (Agents[v]) >= 0&& (ly== "" | | Ly==null) {
            this.location.href= ' http://m.***.com ';  //wap End Address
       }
    }
</script>


The first three methods of this paper are reproduced in http://caibaojian.com/browser-ios-or-android.html

JS to determine if the client is iOS or Android

Related Article

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.