Use JS to determine the four methods you should know by using JS

We always consider whether it is a mobile terminal when writing a response layout, and summarizes 4 methods to determine if the client is iOS or Android. Share out for your reference to study, here, take a detailed introduction with Xiaobian.


1. The first: By judging the use of the browser, use the regular to determine if it is iOS And Android client

User agent Chinese name is part of the HTTP protocol, which belongs to the component of the header field, and the USER Agent also refers to UA. It is a special character serial, an identification of the browser type and version, operating system, and the browser core, and other information to the access website. With this logo, the website accessed by the user can display different typetements to provide users with a better experience or information statistics; for example, using mobile phones to access Google and computer access, these are Google to determine based on the UA of visitors. of. UA can be camouflaged.

The standard format of the UA string of the browser: browser ID (operating system ID; encryption level ID; browser language) rendering engine identification version information. But each browser is different.

VAR u = navigator.USERAGENT; var isandroid = u.indexof (‘Android’)> -1 || u.indexof (‘ADR’)> -1; // android terminal var isios = !! u.match (/ \ (i [^;] +; (u;)? CPU. + Mac OS X /); / / iOS terminal alert (‘is whether it is Android:’ + isandroid; Alert (‘is IOS:’ + ISIOS); [1
 2. Second: Check if it is mobile, ipad, iphone, WeChat, QQ, etc.   The code is as follows:   
// Judgment 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 Core Webkit: U .INDEXOF (‘AppleWebkit’)> -1, // Apple, Google Core Gecko: U.Indexof (‘Gecko’)> -1 && U.Indexof (‘khtml’) == -1, // Firefox core Mobile: !! u.match (/Applewebkit.*Mobile. * /), // Is it 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, // is iphone or qqhd browser iPad: u.indexof (‘ipad’)> -1, // is ipad webapp: u.indexof (‘safari’) ==-1, // Whether the Web should be program, no head and bottom weixin: u.indexof (‘micromessenger’)> -1, // Whether WeChat (2015-01-22 new) QQ: u.match (/ \ SQQ / I) == “QQ” // is QQ};} (), Language: (Navigator.BrowserLanguage || Navigator.Language) .tolowercase ()}

2.2 Using Method

/ Judgment whether IE kernel IF (Browser.Versions.Trident) {Alert (“IS IE”);} // Judgment WEBKIT Kernel IF (Browser.versions.Webkit) {Alert (“is webkit”);} // Judgment whether the mobile IF (Browser.Versions.Mobile || Browser.versions.Android || Browser.versions.ios) {Alert “Mobile”);}

currentlang = navigator.language; // Determine other browsers in addition to IE f (! Currentlang) {// judgment IE browser use language currentlang = navigator.browserlanguage;} Alert (currentlang);

   3. Judgment iPhone | iPad | iPod | iOS | Android client 
The code is as follows:

IF (/( (/( (/( (iPhone|ipad| iPod|ios )/i.test (naVigator.USERAGENT) {// Judging iPhone | ipad | ipod | iOS //Alert (Navigator.USERAGENT); Window.Location.href = “iPhone.html”;} else if (/(Android )/i.test(Navigator.USERAGENT)) {//// Judging Android //Alert (Navigator.USERAGENT); window.location.href = “android.html”;} else {// pc window.location.href = “pc.html”;};
   4. Judgment PC or mobile 
The code is as follows:

// Judging whether the mobile phone is accessible VAR UseERAGENTINFO = Navigator.USERAGENT.TOLOWERCASE (); var Agents = [“Android”, “iPhone”, “Symbianos”, “Windows Phone”, “IPAD”, “iPod”]; var = document .referRer; // Return to navigate to the URL FOR of the page where the hyperlink of the current web page is (var v = 0; v
= 0 && (ly == “|| l == null) {this.location.href = ‘http://m.***.com’; // WAP end address}}
   5. Common jump code 

// borwserRedirect (function browserRedirect () {var sUserAgent = navigator.userAgent.toLowerCase (); var bIsIpad = sUserAgent.match (/ ipad / i) == ‘ipad’; var bIsIphone = sUserAgent .match (/ iPhone OS / I) == ‘iPhone OS’; var bismidp = susegent.match (/ midp / i) == ‘midp’; var bisuc7 = susegent.match (/RV: == ‘RV:’; var bisuc = susegent.match (/ ucWeb / i) == ‘Web’; var bisce = susegent.match (/ windows CE / I) == ‘Windows CE’; var Biswm = SUSERAGENT.MATCH (/ Windows Mobile / i) == ‘Windows Mobile’; var bisandroid = susent.match (/ android / i) == ‘Android’; var pathname = location.pathname if (bisipad || bisiphone || Bismidp || Bisuc7 || Bisuc || Bisce || BiSWM || Bisandroid) {WINDOW.LOCATION.HREF = ‘’+pathname; // WAP end address}} ();
The above is the full content of this article. I hope this paper has a certain reference value for everyone’s learning or work. If you have any questions, please Message exchange, thank you for your support of Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment