网站手机访问自动跳转到WAP手机网站的代码

迅恒数据中心

<script type="text/javascript">
try {
    var urlhash = window.location.hash;
    if (!urlhash.match("fromapp"))
    {
        if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)))
        {
            var docsUrl = "http://" + window.location.host + "/wap";
            window.location= docsUrl
        }
    }
}
catch(err)
{}
</script>

<script type="text/javascript">
try{
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
window.location.href="/wap";
}
}catch(e){}
</script>

 或多重判断:

<script type="text/javascript">
try{
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
window.location.href="/wap";
//判断访问环境是Android|webOS|iPhone|iPod|BlackBerry就访问/wap
}
else if(/iPad/i.test(navigator.userAgent)){
window.location.href="/ipad";
//判断访问环境是iPad平板就访问/ipad
}
else{
window.location.href="/pc";
//如果以上都不是,则访问/pc
}
}catch(e){}
</script>

 或者是:

<script type="text/javascript">
<!--
//平台、设备和操作系统
var system = {
win: false,
mac: false,
xll: false,
ipad: false
};
//检测平台
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
system.ipad = (navigator.userAgent.match(/iPad/i) != null) ? true : false;
//跳转语句电脑或平板访问就自动跳转到/PC
if (system.win || system.mac || system.xll || system.ipad) {
window.location.href="/pc";
}
//非以上跳转到/wap,如果代码放在手机端网站,下面的代码可以去掉
else{
window.location.href="/wap";
}
</script>

 

分类:建站知识 百度收录 必应收录