1. CMS首页
  2. 技术杂文

怎么让网站在微信才能打开 检测微信协议头

  猴大大今天无意中发现一个缩短网址浏览器打开被阻断了,需要在微信客户端才能打开,习惯性的审查元素,发现js代码很简短,可以学习下原理。代码如下:

  <script type="text/javascript"><br>            var ua = navigator.userAgent.toLowerCase();<br>            var isWeixin = ua.indexOf("micromessenger") != -1;<br>            var isAndroid = ua.indexOf("android") != -1;<br>            var isIos = (ua.indexOf("iphone") != -1) || (ua.indexOf("ipad") != -1);<br>            if (!isWeixin) {<br>                document.head.innerHTML = "<title>抱歉,出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css">";<br>                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>";<br>            }<br>        </script>

发表评论

用户名: