zoukankan      html  css  js  c++  java
  • 判断页面在手机和pc打开的方法

    正则和三木

    window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? "https://www.baidu.com/" : "http://news.baidu.com/";

    eg2.

     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
     2 <HTML>
     3  <HEAD>
     4   <TITLE> New Document </TITLE>
     5  </HEAD>
     6  
     7  <BODY>
     8   <SCRIPT LANGUAGE="JavaScript">
     9   <!--
    10 function detectOS(){  
    11     var sUserAgent = navigator.userAgent; 
    12     var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");                                                                        
    13     var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh"); 
    14     if(isMac) return "Mac"; 
    15     var isUnix = (navigator.platform == "X11") && !isWin && !isMac; 
    16     if(isUnix) return "Unix"; 
    17     var isLinux = (String(navigator.platform).indexOf("Linux") > -1); 
    18     if(isLinux) return "Linux"; 
    19     if(isWin) 
    20     {  
    21         var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; 
    22         if(isWin2K) return "Win2000"; 
    23         var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; 
    24         if(isWinXP) return "WinXP"; 
    25         var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; 
    26         if(isWin2003) return "Win2003"; 
    27     }  
    28     return "None"; 
    29 }
    30  
    31 alert(detectOS())
    32  
    33   //-->
    34   </SCRIPT>
    35  </BODY>
    36 </HTML>

    eg3.

     1 function IsPC() {
     2     var userAgentInfo = navigator.userAgent;
     3     var Agents = ["Android", "iPhone",
     4                 "SymbianOS", "Windows Phone",
     5                 "iPad", "iPod"];
     6     var flag = true;
     7     for (var v = 0; v < Agents.length; v++) {
     8         if (userAgentInfo.indexOf(Agents[v]) > 0) {
     9             flag = false;
    10             break;
    11         }
    12     }
    13     return flag;
    14 }

    eg4.

    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";
        if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
            window.location.href=B页面;
        }
    }
    browserRedirect();
  • 相关阅读:
    QT中的定时器使用
    range()函数常和len()函数一起用于字符串索引。在这里我们要显示每一个元素及其索引值。
    range()的print 《P核》P30
    2.13 带逗号的print语句输出的元素之间自动带个空格
    Python3.x和Python2.x的区别
    print语句默认每行添加一个换行符 来自2.13
    2.12 while循环 print与计数器先后顺序对结果的影响
    函数本地绑定与全局绑定的区别
    字典映射{ :}
    《Python核心编程》P21输入数值字符串→转整型
  • 原文地址:https://www.cnblogs.com/sugerxiaoxiao/p/7610450.html
Copyright © 2011-2022 走看看