JS to determine if the client is iOS or Android

Source: Internet
Author: User

Http://caibaojian.com/browser-ios-or-android.html

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, app = navigator.appVersion;var isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1; //android终端或者uc浏览器var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端alert(‘是否是Android:‘+isAndroid);alert(‘是否是iOS:‘+isiOS);</script>

The following is a more comprehensive browser check function, provide more check content, you can check whether it is mobile, ipad, iphone, QQ, etc.

The first type: from http://blog.baiwand.com/?post=176

<script type= "Text/javascript" >//judge Access terminal var browser={versions:function () {var u = navigator.useragent, app        = Navigator.appversion; return {trident:u.indexof (' Trident ') >-1,//ie kernel presto:u.indexof (' presto ') >-1,//opera inside Nuclear webkit:u.indexof (' AppleWebKit ') >-1,//Apple, Google kernel gecko:u.indexof (' Gecko ') >-1 && u . IndexOf (' khtml ') = = -1,//Firefox core mobile:!! U.match (/applewebkit.*mobile.*/),//Whether for Mobile terminal iOS:!! U.match (/\ (i[^;] +;( U;)? Cpu.+mac OS x/),//ios terminal Android:u.indexof (' Android ') >-1 | |            U.indexof (' Linux ') >-1,//android terminal or UC Browser 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 program, no head with bottom        Weixin:u.indexof (' Micromessenger ') >-1,//whether (added in 2015-01-22) qq:u.match (/\sqq/i) = = "QQ"//whether QQ    }; } (), LanguAge: (Navigator.browserlanguage | | navigator.language). toLowerCase ()}</script>

How to use:

//判断是否IE内核if(browser.versions.trident){ alert("is IE"); }//判断是否webKit内核if(browser.versions.webKit){ alert("is webKit"); }//判断是否移动端if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }

Detecting Browser language

currentLang = navigator.language;   //判断除IE外其他浏览器使用语言if(!currentLang){//判断IE浏览器使用语言    currentLang = navigator.browserLanguage;}alert(currentLang);

The second type: from http://www.fufuok.com/js-iphone-android. HTML

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {    //alert(navigator.userAgent);      window.location.href ="iPhone.html";} else if (/(Android)/i.test(navigator.userAgent)) {    //alert(navigator.userAgent);     window.location.href ="Android.html";} else {    window.location.href ="pc.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.