通过JS语句判断WEB网站的访问端是电脑还是手机
JavaScript不管是判断PC浏览器还是手机浏览器,都是通过User Agent 来判断, 腾讯网的适配代码.
<script type="text/javascript">  
if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){ 
    if(window.location.href.indexOf("?mobile")<0){ 
        try{ 
            if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){ 
                window.location.href="http://shipei.qq.com/index.htm"; 
            }else if(/iPad/i.test(navigator.userAgent)){ 
            }else{ 
                window.location.href="http://shipei.qq.com/simple/s/index/" 
            } 
        }catch(e){} 
    } 
} 
</script>
<script type="text/javascript"> 
    var browser = { 
        versions : function() { 
            var u = navigator.userAgent, app = navigator.appVersion; 
            return {//移动终端浏览器版本信息                                  
            trident : u.indexOf('Trident') > -1, //IE内核                                  
            presto : u.indexOf('Presto') > -1, //opera内核                                  
            webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核                                  
            gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核                                 
            mobile : !!u.match(/AppleWebKit.*Mobile.*/) 
                    || !!u.match(/AppleWebKit/), //是否为移动终端                                  
            ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端                  
            android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器                                  
            iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器                     
            iPad: u.indexOf('iPad') > -1, //是否iPad        
            webApp : u.indexOf('Safari') == -1,//是否web应该程序,没有头部与底部 
            google:u.indexOf('Chrome')>-1 
        }; 
    }(), 
    language : (navigator.browserLanguage || navigator.language).toLowerCase() 
    } 
    document.writeln("语言版本: "+browser.language); 
    document.writeln(" 是否为移动终端: "+browser.versions.mobile); 
</script>




声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
ReferenceError: __dirname is not defined in ES module scope
WordPress 文章排序
MySQL的外键总结
MySQL 使用 DATE_FORMAT() 和 FROM_UNIXTIME() 格式化时间
FFmpeg 修改默认音轨
Rollup 教程
二分查找法
wp_signon() 自定义用户登录函数