JS identifies the access browser and determines whether it is android or ios, androidios

Source: Internet
Author: User

JS identifies the access browser and determines whether it is android or ios, androidios

After scanning the QR code in the project, the system automatically identifies the android or ios system and downloads different systems.

<Script type = "text/javascript">/** smartphone browser version: **/var browser = {versions: function () {var u = navigator. userAgent, app = navigator. appVersion; return {// mobile terminal browser version information trident: u. indexOf ('think')>-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. */) | !! U. match (/AppleWebKit/), // whether it is a mobile device 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 | u. indexOf ('mac')>-1, // whether the browser is iPhone or QQHD iPad: u. indexOf ('ipad ')>-1, // whether iPadwebApp: u. indexOf ('safari ') =-1 // whether the web should program, no header and bottom};} (), language: (navigator. browserLanguage | navigator. language ). toLowerCase ()} if (browser. versions. ios | browser. versions. iPhone | browser. versions. iPad) {window. location = "https://itunes.apple.com/xxx";} else if (browser. versions. android) {window. location = "http: // xxx/xxx.apk";} // document. writeln ("language version:" + browser. language); // document. writeln ("whether it is a mobile terminal:" + browser. versions. mobile); // document. writeln ("ios terminal:" + browser. versions. ios); // document. writeln ("android terminal:" + browser. versions. android); // document. writeln ("iPhone:" + browser. versions. iPhone); // document. writeln ("iPad:" + browser. versions. iPad); // document. writeln (navigator. userAgent); </script>


How does js determine whether the client is a mobile terminal such as iOS or Android?

Judgment Principle:
JavaScript is the main language for front-end development. We can compile JavaScript programs to determine the browser type and version. There are two methods to determine the browser type in JavaScript: one is to distinguish based on the unique attributes of various browsers, and the other is to judge by analyzing the userAgent attributes of the browser. In many cases, after determining the browser type, you must determine the browser version to handle compatibility issues. However, you can only determine the browser version by analyzing the browser's userAgent.

Browser type
(1) Special browser attributes
(2) According to userAgent
Browser version
(1) According to userAgent

Mobile browser judgment
1. How to determine whether a mobile terminal uses regular match,
Match navigator. userAgent with the string AppleWebKit ***** Mobile
Android qq browser HD only supports AppleWebKit
2 mobile phone language version judgment
You can use navigator. browserLanguage to obtain the windows phone language version,
Of course, there are also compatibility differences between the hateful little mobile phone language versions, compatible with Mozilla, and AppleWebKit kernel browsers to access their language versions, it will list navigator. language
CODE:
<Script type = "text/javascript">
Var browser = {
Versions: function (){
Var u = navigator. userAgent, app = navigator. appVersion;
Return {// mobile terminal browser version information
Trident: u. indexOf ('think')>-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 ('k') =-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 it is an iPhone or QQHD Browser
IPa ...... remaining full text>
 
How does js determine whether the client is a mobile terminal such as iOS or Android?

Judge the browser
<Script> var browser = navigator. userAgent/* browser. indexOf ('applewebkit ')>-1 // Apple, Google kernel browser. indexOf ('presto ')>-1 // opera kernel browser. indexOf ('think')>-1 // IE kernel browser. indexOf ('gecko ')>-1 & browser. indexOf ('khtml ') =-1 // Firefox kernel !! Browser. match (/AppleWebKit. * Mobile. */) // whether it is a Mobile terminal !! Browser. match (/\ (I [^;] +; (U ;)? CPU. + Mac OS X/) // ios browser. indexOf ('android')>-1 | browser. indexOf ('linux ')>-1 // android terminal or uc browser. indexOf ('iphone ')>-1 // whether the browser is iPhone or QQHD. indexOf ('ipad ')>-1 // whether iPad */</script> is expected to be useful to you.


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.