js determines whether the current page is open on a mobile device or on the PC side

  • 2020-11-18 06:06:36
  • OfStack

This article explains the detailed code of js to determine whether the current page is opened on a mobile device or in PC, and shares it with you for your reference. The specific content is as follows


 var browser = {
              versions: function () {
                var u = navigator.userAgent, app = navigator.appVersion;
                return {     // Mobile terminal browser version information 
                  trident: u.indexOf('Trident') > -1, //IE The kernel 
                  presto: u.indexOf('Presto') > -1, //opera The 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.*/), // Is it a mobile terminal 
                  ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios terminal 
                  android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android A terminal or uc The browser 
                  iPhone: u.indexOf('iPhone') > -1, // Whether it is iPhone or QQHD The browser 
                  iPad: u.indexOf('iPad') > -1, // Whether or not iPad
                  webApp: u.indexOf('Safari') == -1 // Whether or not web Should program, no head with bottom 
                };
              }(),
              language: (navigator.browserLanguage || navigator.language).toLowerCase()
            }            
            if (browser.versions.mobile) {// Determine if a mobile device is on. browser The code is below 
                var ua = navigator.userAgent.toLowerCase();// Gets the object used for the judgment 
                if (ua.match(/MicroMessenger/i) == "micromessenger") {
                    // Open it in WeChat 
                   setInterval(WeixinJSBridge.call('closeWindow'),2000);
                }
                if (ua.match(/WeiBo/i) == "weibo") {
                    // Open on sina Weibo account 
                }
                if (ua.match(/QQ/i) == "qq") {
                    // in QQ Open the space 
                }
                if (browser.versions.ios) {
                    // Whether in IOS Browser opens 
                } 
                if(browser.versions.android){
                    // Whether to open it in android browser 
                }
            } else {
                // Otherwise it is PC Browser opens 
                window.close();
            }

Code 2: js determines whether the user's browsing device is mobile or PC

Recently, a website page needs to display different page styles according to the different devices visited by users, which is mainly used to determine whether mobile devices or computer browsers are visited.

The js decision processing code is given below for reference.


<script type="text/javascript">
    function browserRedirect() {
      var sUserAgent = navigator.userAgent.toLowerCase();
      var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
      var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
      var bIsMidp = sUserAgent.match(/midp/i) == "midp";
      var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
      var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
      var bIsAndroid = sUserAgent.match(/android/i) == "android";
      var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
      var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
      document.writeln(" Your browsing device is: ");
      if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        document.writeln("phone");
      } else {
        document.writeln("pc");
      }
    }

    browserRedirect();
  </script>

I used the browser on the computer, android device, iphone, ipad have all done the test, this code is feasible, each device is correct.

Above is the entire content of this article, I hope to help you with your study.


Related articles: