用js限制网页只在微信浏览器中打开(或者只能手机端访问)

前端之家收集整理的这篇文章主要介绍了用js限制网页只在微信浏览器中打开(或者只能手机端访问)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用js限制网页只在微信浏览器中打开

js

$(function(){
	//判断页面是否是在微信浏览器打开
	//对浏览器的UserAgent进行正则匹配,不含有微信独有标识的则为其他浏览器
  var useragent = navigator.userAgent;
  if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
    window.location.href = "wxError.html";//若不是微信浏览器,跳转到温馨error页面
  }
})

页面

<!DOCTYPE html>
<html>
  <head>
  	<Meta charset="utf-8">
  	<Meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  </head>
  <body>
    <script type="text/javascript">
      var ua = navigator.userAgent.toLowerCase();
      var isWeixin = ua.indexOf('micromessenger') != -1;
      var isAndroid = ua.indexOf('android') != -1;
      var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
      if (!isWeixin) {
        document.head.innerHTML = '<title>抱歉,出错了</title><Meta charset="utf-8"><Meta name="viewport" content="width=device-width,user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css" rel="external nofollow" >';
        document.body.innerHTML = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">请在微信客户端打开链接</h4></div></div>';
      }
    </script>
  </body>
</html>

一般情况下限制页面被pc端访问就可以了,那么就可以参考下面的代码

在需要被pc端访问的页面的脚本里加上如下代码即可:

<script>
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 = "提示页面路径";
  }
}

</script>

到这里就完美了,我们小编专门从电影网站扒的代码

原文链接:https://www.f2er.com/js/534644.html

猜你在找的JavaScript相关文章