【前端】微信H5,js判断是否在微信浏览器中打开

 微信H5,Js判断是否在微信浏览器中打开

<script type="text/javascript">
    window.onload = function() {
        var weChart = false
        var ios1 = document.getElementById('ios')
        var android1 = document.getElementById('android')
        var iosHref = ios1.getAttribute('href')
        var androidHref = android1.getAttribute('href')
        var agent = navigator.userAgent.toLowerCase();
        if(agent.match(/MicroMessenger/i) == 'micromessenger') {
            weChart = true
        } else {
            weChart = false
        }
        function detect(){
            let equipmentType = "";
            let android = agent.indexOf("android");
            let iphone = agent.indexOf("iphone");
            let ipad = agent.indexOf("ipad");
            if(android != -1){
                equipmentType = "android";
            }
            if(iphone != -1 || ipad != -1){
                equipmentType = "ios";
            }
            return equipmentType;
        }
        if(detect()==='android'  && weChart === false){
            //对Android系统的移动端页面做点什么
            ios1.href = androidHref
            document.getElementById('mask').style.display = 'none'
        } else if(detect()==='android' && weChart === true) {
            ios1.href = androidHref
            document.getElementById('mask').style.display = 'block'
        } else if(detect()==='ios'){
            //对IOS系统的移动端页面做点什么
            android1.href = iosHref
            document.getElementById('mask').style.display = 'none'
        }
    }
</script>

完结!

猜你喜欢

转载自blog.csdn.net/weixin_44299027/article/details/110825667