JS to determine whether the client is iOS or Android mobile terminals such as the method of _javascript skills

Source: Internet
Author: User
principle of judgment:

JavaScript is the primary language for front-end development, and we can write JavaScript programs to determine the type and version of the browser. JavaScript can determine browser types in general there are two ways, one is based on a variety of browser-specific properties to distinguish, the other is by analyzing the browser's useragent properties to judge. In many cases, after the value is judged by the browser type, it is also necessary to judge the browser version to handle the compatibility issue, and to judge the browser version is generally only through the analysis of the browser useragent to know.

Browser Type

⑴ Browser-specific properties
⑵ according to UserAgent
Browser version
⑴ according to UserAgent

for mobile browsers to judge

1. How to determine whether to use the regular match for mobile terminals,
Matches whether navigator.useragent contains strings Applewebkit*****mobile
Android QQ Browser HD edition only AppleWebKit

2 Mobile language version of the judgment
Use Navigator.browserlanguage to get the Windows Phone language version,
Of course, hateful little phone. Language versions also have compatibility differences, compatible with Mozilla, and AppleWebKit kernel browsers to access their language versions, which list Navigator.language
CODE:
Copy Code code as follows:

<script type= "Text/javascript" >
var browser={
Versions:function () {
var u = navigator.useragent, app = Navigator.appversion;
return {//Mobile terminal browser version information
Trident:u.indexof (' Trident ') >-1,//ie kernel
Presto:u.indexof (' presto ') >-1,//opera kernel
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 (' Linux ') >-1,//android terminal or UC Browser
Iphone:u.indexof (' iphone ') >-1,//whether for IPhone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether the ipad
Webapp:u.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
};
}(),
Language: (Navigator.browserlanguage | | navigator.language). toLowerCase ()
}
Document.writeln ("language version:" +browser.language);
Document.writeln ("Whether for Mobile Terminal:" +browser.versions.mobile);
Document.writeln ("iOS terminal:" +browser.versions.ios);
Document.writeln ("Android Terminal:" +browser.versions.android);
Document.writeln ("Whether for IPhone:" +browser.versions.iphone);
Document.writeln ("Whether the IPad:" +browser.versions.ipad);
Document.writeln (navigator.useragent);
</script>

More special place.
UC Browser does not have an Android header, only return: Linux, here roughly based on Linux to judge is Android (the premise must be satisfied with the mobile terminal, UC this is satisfied)
Android QQ Browser HD version of the results of the test: Mac, Safari
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.