美文网首页
js限制网站只能在手机或者微信内访问

js限制网站只能在手机或者微信内访问

作者: 10676 | 来源:发表于2022-08-31 21:47 被阅读0次
  //以下是限制只能在微信内置浏览器访问
   var useragent = navigator.userAgent;
      if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
          // 这里警告框会阻塞当前页面继续加载
          alert('请使用微信访问!');
          // 以下代码是用javascript强行关闭当前页面
          var opened = window.open('about:blank', '_self');
          opened.opener = null;
          opened.close();
     }
 
 
 //以下是限制只能在手机上访问
 var system = {};
 var p = navigator.platform;
 var u = navigator.userAgent;
  
 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) {//如果是PC转 
     if (u.indexOf('Windows Phone') > -1) {  //win手机端
  
     }
     else {
         window.location.href = "http://weixin.sogou.com";
     }
 }
     }
限制只能PC端访问
<script type="text/javascript"> 

    //平台、设备和操作系统 

    var system ={ 
        win : false, 
        mac : false, 
        xll : false
    };

    //检测平台
    var p = navigator.platform; 
    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){   
 
    }else{ 
       window.location.href="pc端访问的链接";
    }
</script>

简化一下

    var p = navigator.platform; 
    if(p.indexOf("Win")<0){   
        alert("请在PC端使用");
    }else{ 
       window.location.href="pc端访问的链接";
    }

相关文章

网友评论

      本文标题:js限制网站只能在手机或者微信内访问

      本文链接:https://www.haomeiwen.com/subject/tvfjnrtx.html