判断是否在微信浏览器打开

微信官方的写法

<!DOCTYPE html>
<html>
    <head>
        <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;
            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, 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">';
                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>

  

夏柔的写法

<!DOCTYPE html>
<html>
 
<head>
    <title>抱歉,出错了</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
    <style>
        @font-face {
            font-family: 'iconfont';
            /* Project id 3213278 */
            src: url('//at.alicdn.com/t/font_3213278_flhy4e6wbjg.woff2?t=1646127490384') format('woff2'), url('//at.alicdn.com/t/font_3213278_flhy4e6wbjg.woff?t=1646127490384') format('woff'), url('//at.alicdn.com/t/font_3213278_flhy4e6wbjg.ttf?t=1646127490384') format('truetype');
        }
        
        .iconfont {
            font-family: "iconfont";
            font-size: 14px;
            font-style: normal;
        }
    </style>
</head>
 
<body>
    <script type="text/javascript">
        var ua = navigator.userAgent.toLowerCase();
        var isWeixin = ua.indexOf('micromessenger') != -1;
        var ua = navigator.userAgent;
        var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
            isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
            isAndroid = ua.match(/(Android)\s+([\d.]+)/),
            isMobile = isIphone || isAndroid;
        if (isMobile) {
            if (isWeixin) {
                document.body.innerHTML = '<div id="container" style="display: block;"><div style="position:relative;padding-top:140px;color:#333;font-size: 20px;text-align: center;"><i style="position: absolute;top: 10px;right: 40px;font-size: 80px" class="iconfont icon-jiantou"></i><p style="font-weight: 600;">请点击屏幕右上角 [ ··· ]</p><p>在<i style="font-size: 30px;color: #1487F0" class="iconfont icon-browser"></i> 浏览器打开网页</p><br><p style="color:red;">请在浏览器打开本网页哦~</p></div></div>';
            }
        }
    </script>
</body>
 
</html>

  文章转自https://www.wpon.cn/26303.html

posted @ 2023-03-01 14:54  小白咚  阅读(59)  评论(0编辑  收藏  举报