Use js to determine whether the client is a mobile phone or a PC

From , 5 Years ago, written in JavaScript, viewed 128 times.
URL https://pastebin.vip/view/f73b76ce
  1. //方法1:
  2. function IsPC() {
  3.     var userAgentInfo = navigator.userAgent;
  4.     var Agents = ["Android", "iPhone",
  5.                 "SymbianOS", "Windows Phone",
  6.                 "iPad", "iPod"];
  7.     var flag = true;
  8.     for (var v = 0; v < Agents.length; v++) {
  9.         if (userAgentInfo.indexOf(Agents[v]) > 0) {
  10.             flag = false;
  11.             break;
  12.         }
  13.     }
  14.     return flag;
  15. }
  16.  
  17.  
  18. //方法2:
  19. function browserRedirect() {
  20.     var sUserAgent = navigator.userAgent.toLowerCase();
  21.     var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  22.     var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  23.     var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  24.     var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  25.     var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  26.     var bIsAndroid = sUserAgent.match(/android/i) == "android";
  27.     var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  28.     var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  29.     if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
  30.         window.location.href=B页面;
  31.     }
  32. }
  33. browserRedirect();
  34.  
  35. //方法3:
  36. <script type="text/javascript">
  37.     var browser = {
  38.         versions : function() {
  39.             var u = navigator.userAgent, app = navigator.appVersion;
  40.             return {//移动终端浏览器版本信息                                
  41.             trident : u.indexOf('Trident') > -1, //IE内核                                
  42.             presto : u.indexOf('Presto') > -1, //opera内核                                
  43.             webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核                                
  44.             gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核                                
  45.             mobile : !!u.match(/AppleWebKit.*Mobile.*/)
  46.                     || !!u.match(/AppleWebKit/), //是否为移动终端                                
  47.             ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端                
  48.             android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器                                
  49.             iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器                    
  50.             iPad: u.indexOf('iPad') > -1, //是否iPad      
  51.             webApp : u.indexOf('Safari') == -1,//是否web应该程序,没有头部与底部
  52.             google:u.indexOf('Chrome')>-1
  53.         };
  54.     }(),
  55.     language : (navigator.browserLanguage || navigator.language).toLowerCase()
  56.     }
  57.     document.writeln("语言版本: "+browser.language);
  58.     document.writeln(" 是否为移动终端: "+browser.versions.mobile);
  59. </script>

Reply to "Use js to determine whether the client is a mobile phone or a PC"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website