JS判断终端设备跳转PC端、移动端相应的URL

<!DOCTYPE html>
<html>
<head>
    <meta charset=" utf-8">
    <meta name="author" content="http://www.jb51.net/" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <meta content="black-translucent" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <script src="jq.min.js"></script>
    <script>
        function sp_isMobile() {
            return Boolean(navigator.userAgent.match(/.*(iphone|ipod|android|symbian|nokia|blackberry| rim |opera mini|opera mobi|windows ce|windows phone|up\.browser|netfront|palm-|palm os|pre\/|palmsource|avantogo|webos|hiptop|iris|kddi|kindle|lg-|lge|mot-|motorola|nintendo ds|nitro|playstation portable|samsung|sanyo|sprint|sonyericsson|symbian).*/i) || navigator.userAgent.match(/alcatel|audiovox|bird|coral|cricket|docomo|edl|huawei|htc|gt-|lava|lct|lg|lynx|mobile|lenovo|maui|micromax|mot|myphone|nec|nexian|nook|pantech|pg|polaris|ppc|sch|sec|spice|tianyu|ustarcom|utstarcom|videocon|vodafone|winwap|zte/i));
        }
        //移动端排除ipad
        if(sp_isMobile()&&location.search.indexOf('pc=true')==-1&&navigator.userAgent.indexOf('iPad')==-1) {
            //添加meta 方便移动端获取document.documentElement.clientWidth
            document.write('<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />');

            //
            var tabIndex = getRequestMini('Device');
            //alert(tabIndex);
            if(tabIndex == "viewPc" || document.documentElement.clientWidth>760){
                location.href="http://www.baidu.com/";
            }else{
                //location.href="http://u.pingan.com/upingan/upingan/index.html?mediasource=SC03-Direct-00002";
                location.href="http://www.jb51.net/";
            }
            //格式化URL传进来的参数
            function getRequestMini(name) {
                return (location.search.match(new RegExp('[&?]' + name + '=([^&]*)', 'i')) || [''])[0].split('=')[1];
            };
        }

    </script>
    <script type="text/javascript">
     //
    </script>
    <title>test</title>
</head>
<body>
    <div style="width:500px; height: 500px; border: 1px solid #ccc; text-align: center;">
        JS终端设备检测
    </div>
</body>
</html>

 

posted @ 2015-12-25 21:10  iicoo  阅读(620)  评论(0编辑  收藏  举报