本站前面分享过最靠谱的判断浏览器类型JS方法,今天要分享的是判断微信内置浏览器的JS代码方法,原理是通过判断UA中是否有关键字micromessenger,有的话就是微信内置浏览器。以下为实例,这也是腾讯微信官方某些网页用的判断代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"> </head> <body> <script type="text/javascript"> var ua = navigator.userAgent.toLowerCase(); var isWeixin = ua.indexOf('micromessenger') != -1; if (!isWeixin) { 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/connect/zh_CN/htmledition/style/wap_err1a9853.css">'; document.body.innerHTML = '<div class="page_msg"><div class="inner"><span class="msg_icon_wrp"><i class="icon80_smile"></i></span><div class="msg_content">请在微信客户端打开链接</div></div></div>'; } </script> </body> </html>
示例实际效果: