当前位置:网站首页>JS judge the mobile terminal and PC terminal

JS judge the mobile terminal and PC terminal

2022-06-23 02:56:00 User 1349575

navigator Object contains all the information about the browser , All browsers are supported .

navigator Object has a property of userAgent, This is a read-only string , The browser is declared for HTTP Value of the requested user agent header . So we can judge navigator.userAgent Whether it contains some values to judge . The following for userAgent Print value of .

Method 1 :

function IsPC(){  
     var userAgentInfo = navigator.userAgent;
     var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");  
     var flag = true;  
     for (var v = 0; v < Agents.length; v++) {  
         if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }  
     }  
     return flag;  
  }

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    //alert(navigator.userAgent);  
    window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
    //alert(navigator.userAgent); 
    window.location.href ="Android.html";
} else {
    window.location.href ="pc.html";
};

Method 2 :

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.*/), // 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 Terminal or uc browser 
                iPhone: u.indexOf('iPhone') > -1 , // Is it iPhone perhaps QQHD browser 
                iPad: u.indexOf('iPad') > -1, // whether iPad  
                webApp: u.indexOf('Safari') == -1, // whether web It should be programmed , No head and bottom 
                weixin: u.indexOf('MicroMessenger') > -1, // Wechat or not  
                qq: u.match(/\sQQ/i) == " qq" // whether QQ
            };
         }(),
         language:(navigator.browserLanguage || navigator.language).toLowerCase()
} 
 
  if(browser.versions.mobile || browser.versions.ios || browser.versions.android || 
  	browser.versions.iPhone || browser.versions.iPad){  	
   		window.location = "http://m.abc.com";	
  }

Method 3 : Excerpt from im- qq

var os = function() {
     var ua = navigator.userAgent,
     isWindowsPhone = /(?:Windows Phone)/.test(ua),
     isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone, 
     isAndroid = /(?:Android)/.test(ua), 
     isFireFox = /(?:Firefox)/.test(ua), 
     isChrome = /(?:Chrome|CriOS)/.test(ua),
     isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
     isPhone = /(?:iPhone)/.test(ua) && !isTablet,
     isPc = !isPhone && !isAndroid && !isSymbian;
     return {
          isTablet: isTablet,
          isPhone: isPhone,
          isAndroid : isAndroid,
          isPc : isPc
     };
}();
 
if(os.isAndroid || os.isPhone){
    alert("-----");
}

Method four :

if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
    if(window.location.href.indexOf("?mobile")<0){
        try{
            if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
                window.location.href=" Mobile page ";
            }else if(/iPad/i.test(navigator.userAgent)){
                window.location.href=" Tablet page ";
            }else{
                window.location.href=" Other mobile terminal pages "
            }
        }catch(e){}
    }
}

Method five :

<script language="javascript"> 
// platform 、 Equipment and operating system   
var system ={  
    win : false,  
    mac : false,  
    xll : false  
};  
// Detection platform   
var p = navigator.platform;  
system.win = p.indexOf("Win") == 0;  
system.mac = p.indexOf("Mac") == 0;  
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);  
// Jump statements , If it is mobile access, it will automatically jump to abc.com page   
if(system.win||system.mac||system.xll){  
 
}else{  
    window.location.href="http://abc.com";  
}  
</script>

Methods six :

<script language="javascript"> 
function is_mobile() {
  var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
  var u = navigator.userAgent;
  if (null == u) {
   return true;
  }
  var result = regex_match.exec(u);
 
  if (null == result) {
   return false
  } else {
   return true
  }
 }
 if (is_mobile()) {
  document.location.href= 'http://domain.com';  // modify http://domain.com Jump to the target page address for you 
 }
</script>


 

Method 7: Baidu webapp edition

<!--- Identify mobile phones or computers js Start --->
<script language="javascript"> 
(function(){
    var res = GetRequest();
    var par = res['index'];
    if(par!='gfan'){
        var ua=navigator.userAgent.toLowerCase();
        var contains=function (a, b){
            if(a.indexOf(b)!=-1){return true;}
        };
// The following http://abc.com Change it to yours wap Mobile page address   As my  http://abc.com
        var toMobileVertion = function(){
            window.location.href = 'http://abc.com/'
        }
 
        if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){return false}
        if((contains(ua,"android") && contains(ua,"mobile"))||(contains(ua,"android") && contains(ua,"mozilla")) ||(contains(ua,"android") && contains(ua,"opera"))
    ||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();}
    }
})();
function GetRequest() {
   var url = location.search; // obtain url in "?" The string after the sign 
   var theRequest = new Object();
   if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}
</script>
<!--- Identify mobile phones or computers js end --->
原网站

版权声明
本文为[User 1349575]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201261219135235.html