JS实现手机访问pc网址自动跳转到wap网站

    之前写pc端直接跳转wap端一直是后端java写的,跟js一样都是根据navigator.userAgent来判断设备是电脑还是手机的,我知道这种前端也可已完成的功能,只是后台比较强势,本人本着以和为贵的精神就没有跟他争执。

不过我还是比较喜欢前端判断设备的,不需要经过服务器编译,减少服务器压力(个人认为,可能有错,非喜勿喷)。下面我就写一段用js判断设备的代码,其中有些资料是查网上的。

    //设备检测
    function detectmob() {
        if( navigator.userAgent.match(/Android/i)
                || navigator.userAgent.match(/webOS/i)
                || navigator.userAgent.match(/iPhone/i)
                || navigator.userAgent.match(/iPad/i)
                || navigator.userAgent.match(/iPod/i)
                || navigator.userAgent.match(/BlackBerry/i)
                || navigator.userAgent.match(/Windows Phone/i)
                ){
            return true;
        }
        else {
            return false;
        }
    }

    if(detectmob()){
                window.location.href="你想跳转的wap页面"
    }

这是不是很容易。

原文地址:https://www.cnblogs.com/a8497336/p/7232290.html