zoukankan      html  css  js  c++  java
  • 用JS判断用户使用的是手机端还是pc端访问

      最近项目中用到一个应用,当访问同一个网站地址的时候,例如:www.xxx.com的时候,如果当前客户端是pc则跳转到专注于pc的部分,如果当前客户机是手机,则跳转到专注于手机的部分,秉承一贯的习惯,baidu or google,但发觉网上的解决办法都不尽如人意,很多都是通过js读取本地文件系统进行判断,但经过测试,不能成功,而且通过js读取本地文件系统会造成安全性问题,但作为开放的互联网,我们不可能为每一部电脑设置安全性,于是自己动手,丰衣足食,以下就是我的解决办法: 
      依然是用js,不过只需要用到 navigator.platform,这是鉴于读取这个属性并不会造成安全性问题,而且,普遍的操作系统都屈指可数 
       ~~~navigator.platform判断系统类型,从而判断是手机还是pc
      简单的跳转代码如下: 
        if(navigator.platform.indexOf('Win32')!=-1){ 
            //go to pc 
         }else{ 
            // go to 手机 
          } 
     

    -------------------------------------------------------

    另一种方法  ~~~用 navigator.userAgent 判断是否现在的手机浏览器,似乎 navigator.platform 比较简单靠谱一点

    <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";
    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
    window.location.href=B页面;
    }
    }
    browserRedirect(); </script>

    -------------------------------------------------------


    用户是手机访问还是电脑方法 ~~~还是用 navigator.userAgent去判断是否为手机浏览器,不过这个比较清晰一点

     代码如下 复制代码 
    var is_iPd = navigator.userAgent.match(/(iPad|iPod|iPhone)/i) != null;
    var is_mobi = navigator.userAgent.toLowerCase().match(/(ipod|iphone|android|coolpad|mmp|smartphone|midp|wap|xoom|symbian|j2me|blackberry|win ce)/i) != null;
    if(is_mobi && window.location.search.indexOf("mv=fp")<0){
    window.location.href="#";
    }
     

    浏览器类型

     代码如下 复制代码 
    if(navigator.userAgent.indexOf("MSIE")>0){
       //ie
       }else if(navigator.userAgent.indexOf("Firefox")>0){
       //firefox
       }else if(navigator.userAgent.indexOf("Chrome")>0){
       //chrome
       }else if(navigator.userAgent.indexOf("Safari")>0){
       //safari
       }else{
       //this part can be used as opera area
       }

    <script type="text/javascript">  
     
        <!--  
     
        //平台、设备和操作系统   
     
        var system ={  
     
            win : false,  
     
            mac : false,  
     
            xll : false 
     
        };  
     
        //检测平台   
     
        var p = navigator.platform;  
     
        alert(p);  
     
        system.win = p.indexOf("Win") == 0;  
     
        system.mac = p.indexOf("Mac") == 0;  
     
        system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);  
     
        //跳转语句   
     
        if(system.win||system.mac||system.xll){//转向后台登陆页面  
     
            window.location.href="login.jsp";  
     
        }else{  
     
            window.location.href="wapLogin.jsp";  
     
        }  
     
        -->  
     
    </script>

  • 相关阅读:
    响应式网页设计初探
    karma 启动提示PhantomJS not found on PATH
    Vuex 拾遗
    cStringIO 实现指定大小的字符串缓存
    javascript 计算文件MD5 浏览器 javascript读取文件内容
    springmvc值传递
    SpringMVC
    spring事务
    spring整合JDBC
    spring-test与junit
  • 原文地址:https://www.cnblogs.com/stephenykk/p/3364848.html
Copyright © 2011-2022 走看看