24
2015
11

手机移动端访问pc版网站自动跳转到手机版js代码

 代码1:

<script type="text/javascript"> 
    function browserRedirect() { 
        var sUserAgent = navigator.userAgent.toLowerCase();
        var Android = sUserAgent.match(/android/i) == "android";
        var Linux =  sUserAgent.match(/linux/i) == "linux";
        var Mobi = sUserAgent.match(/mobi/i) == "mobi";
        var Pad = sUserAgent.match(/pad/i) == "pad";
        var IOS = sUserAgent.match(/ios/i) == "ios";
        var iPad = sUserAgent.match(/ipad/i) == "ipad";
        var iPhone = sUserAgent.match(/iphone/i) == "iphone";
        var Windows_Phone = sUserAgent.match(/windows iphone/i) == "windows iphone";
        var Symbian = sUserAgent.match(/symbian/i) == "symbian";
         
        if ((Android || Linux) && Mobi && !Pad || (IOS && !iPad) || iPhone || Windows_Phone || Symbian){
            var curHref = window.location.href;
            if (curHref.indexOf("http://***.com") == 0 ){
                window.location.href=curHref.replace("http://***.com","http://m.***.com");
            }
            if (curHref.indexOf("http://www.***.com") == 0 ){
                window.location.href=curHref.replace("http://www.***.com","http://m.***.com");
            }
        }
    } 
    browserRedirect();
</script>


代码2:

 <script type="text/javascript">
// JavaScript Document
function urlredirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
        // PC跳转移动端
        var thisUrl = window.location.href;
        window.location.href = thisUrl.replace('www','m');
        
    }
}
urlredirect();
</script>

 

« 上一篇下一篇 »

发表评论: