百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js判断手机或者PC的几个例子
分享文章到:

js判断手机或者PC的几个例子

发布时间:01/15 来源: 浏览: 关键词:
js判断手机或者PC的例子我们在几乎所有网站都会有这段代码了,现在手机流量与pc差不多了,下面来看两段js判断手机或者PC例子吧.

例子一

<script>
function browserRedirect() {
    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) ){
     window.location.href='http://www.111cn.net';
    }else{
  //window.location.href='http://m.111cn.net'; 
 }
}
$(function(){
 browserRedirect();
});
</script>

例子2

<script type="text/javascript">
/*
    PC841适配跳转
*/
(function(){
    var reWriteUrl = function(url){
        if(url){
            var Splits = url.split("/"),siteName=window.location.pathname;
            if(typeof siteName!=="undefined"){
                return "http://m.111cn.net"+siteName;
            }
        }
    };
    if(/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile/i.test(navigator.userAgent)){
        var url=window.location.href;
        var pathname=window.location.pathname;
        if(url.indexOf("?mobile")<0){
        try{
            window.location.href=reWriteUrl(url);
        }catch(e){}
        }
    };
})();
</script>


注意,如果给搜索引擎看的话我们在文件头加入

<meta http-equiv="mobile-agent" content="format=xhtml; url=http://m.111cn.net" />
<meta http-equiv="mobile-agent" content="format=html5; url=http://m.111cn.net" />

意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有9人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板