www网址判断手机跳m

<script src="/style/js/jumpmobile.js" type="text/javascript"></script>
<script>
    redirectMobile();
</script>
*强调内容*/自动跳m端
redirectMobile = function() {
    var nowUrl      = location.href,  
        nowHost     = location.host,
        nowPathname = location.pathname,
        jumpUrl     = '',
        mobileUrl   = 'http://m.danji6.com';
        mobileHurl  =  GetUrlRelativePath()
        var sUserAgent= navigator.userAgent.toLowerCase(); 

        var bIsIpad= sUserAgent.match(/ipad/i) == "ipad"; 

        var bIsIphoneOs= sUserAgent.match(/iphone os/i) == "iphone os"; 

        var bIsMidp= sUserAgent.match(/midp/i) == "midp"; 

        var bIsUc7= sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; 

        var bIsUc= sUserAgent.match(/ucweb/i) == "ucweb"; 

        var bIsAndroid= sUserAgent.match(/android/i) == "android"; 

        var bIsCE= sUserAgent.match(/windows ce/i) == "windows ce"; 

        var bIsWM= sUserAgent.match(/windows mobile/i) == "windows mobile"; 
        if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {  try {

            if (nowUrl == 'http://www.danji6.com/') {
                jumpUrl = mobileUrl;
            }else if (nowHost != 'm.danji6.com') {
                var match = nowPathname.match(/^\/[a-zA-Z0-9]*\/(.*)?/i);
                if (match != null) jumpUrl = mobileUrl + mobileHurl;
            }

            if (jumpUrl) location.href = jumpUrl;

        } catch (e) { }
    }
}

function GetUrlRelativePath()
{
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
    if(relUrl.indexOf("?") != -1){
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}
posted @ 2017-09-18 19:04  蜗牛使劲冲  阅读(8)  评论(0编辑  收藏  举报  来源