js judges the jump between PC terminal and mobile terminal

  • 2021-08-05 08:03:21
  • OfStack

I saw a lot of such similar codes on the Internet, but some of them are very complicated, or some of them are not completely judged. The last time the manager came back from seeing customers, he used Apple Browse to open pc (pc has already made a recognition jump) and will automatically jump to the mobile page. Later, it was discovered after testing

document. writeln ("Mobile or not:" + browser. versions. mobile + " < /br > "); //Print out true

Therefore, in the full version of the code, the first layer if judges that 1 is directly true

The above reason is that the judgment circulating on the Internet is:

mobile:! ! u.match (/AppleWebKit.*Mobile.*/)! ! u. match (/AppleWebKit/), whether//is a mobile terminal

Incomplete judgment will cause this reason.

Below this site, this site shares the judgment codes commonly used by websites for everyone

pc automatic hop mobile terminal


(function() {
      if (/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) {
        var siteName = window.location.pathname;
        if (url.indexOf("?pc") < 0) {
          try {
				if (typeof siteName !== "undefined") {
            	window.location.href = "https://m.ofstack.com" + siteName
          		} 
          } catch (e) {}
        }
      }
    })();

The mobile terminal automatically jumps the pc terminal


;(function() {
  var reWriteUrl = function(url) {
    if (url) {
      var Splits = url.split("/"),
      siteName = window.location.pathname;
      if (typeof siteName !== "undefined") {
        return "https://www.ofstack.com" + siteName
      }
    }
  };
  if (!/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) {
    var url = window.location.href;
    var pathname = window.location.pathname;
    if (url.indexOf("?m") < 0) {
      try {
        window.location.href = reWriteUrl(url)
      } catch(e) {}
    }
  }
})();

The correct judgment should be:


mobile: !! u.match(/AppleWebKit.*Mobile.*/) || !! u.match(/AppleWebKit/) && u.indexOf('QIHU') && u.indexOf('QIHU') > -1 && u.indexOf('Chrome') < 0, // Whether it is a mobile terminal 

Test program code


var browser = {
 versions: function() {
 var u = navigator.userAgent;
 return {
  trident: u.indexOf('Trident') > -1,
  presto: u.indexOf('Presto') > -1,
  webKit: u.indexOf('AppleWebKit') > -1,
  gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,
  mobile: !! u.match(/AppleWebKit.*Mobile.*/) || !! u.match(/AppleWebKit/) && u.indexOf('QIHU') && u.indexOf('QIHU') > -1 && u.indexOf('Chrome') < 0,
  ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
  android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
  iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,
  iPad: u.indexOf('iPad') > -1,
  webApp: u.indexOf('Safari') == -1
 }
 } (),
 language:(navigator.browserLanguage || navigator.language).toLowerCase()
};
document.writeln(" Language version : "+browser.language+"</br>");
document.writeln("  Whether it is a mobile terminal : "+browser.versions.mobile+"</br>");
document.writeln(" ios Terminal : "+browser.versions.ios+"</br>");
document.writeln(" android Terminal : "+browser.versions.android+"</br>");
document.writeln("  Whether it is iPhone: "+browser.versions.iPhone+"</br>");
document.writeln("  Whether or not iPad: "+browser.versions.iPad+"</br>");
document.writeln(navigator.userAgent+"</br>");

Full version, applied to project code


/*
*
*  Judge PC End and WAP End 
*/
var mobile_bs = {
 versions: function() {
 var u = navigator.userAgent;
 return {
  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.*/) || !! u.match(/AppleWebKit/) && u.indexOf('QIHU') && u.indexOf('QIHU') > -1 && u.indexOf('Chrome') < 0, // 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 || u.indexOf('Mac') > -1, // Whether it is iPhone Or QQHD Browser 
  iPad: u.indexOf('iPad') > -1, // Whether or not iPad
  webApp: u.indexOf('Safari') == -1 // Whether or not web Should program, no head and bottom 
 }
 } ()
};
if (mobile_bs.versions.mobile) {
 if (mobile_bs.versions.android || mobile_bs.versions.iPhone || mobile_bs.versions.iPad || mobile_bs.versions.ios) {
 window.location.href = " Mobile URL ";
 }
}; 

Related articles: