JS判断是手机访问网站还是电脑访问网站

Jonty
2013-08-04 / 0 评论 / 94 阅读 / 正在检测是否收录...

代码如下:

<script type="text/javascript" language="javascript">// <![CDATA[

(function(){
        var res = GetRequest();
        var par = res['index'];
        if(par!='gfan'){
                var ua=navigator.userAgent.toLowerCase();
                var contains=function (a, b){
                        if(a.indexOf(b)!=-1){return true;}
                };
//将下面的http://wap.nobb.cc/改成你的wap手机版地址 
                var toMobileVertion = function(){
                        window.location.href = 'http://wap.nobb.cc/'
                }

                if(contains(ua,"ipad")||(contains(ua,"rv:1.2.3.4"))||(contains(ua,"0.0.0.0"))||(contains(ua,"8.0.552.237"))){return false}
                if((contains(ua,"android") &#038;& contains(ua,"mobile"))||(contains(ua,"android") &#038;& contains(ua,"mozilla")) ||(contains(ua,"android") &#038;& contains(ua,"opera"))
        ||contains(ua,"ucweb7")||contains(ua,"iphone")){toMobileVertion();}
        }
})();
function GetRequest() {
   var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&#038;");
      for(var i = 0; i < strs.length; i ++) {
         theRequest[strs.split("=")[0]]=unescape(strs.split("=")[1]);
      }
   }
   return theRequest;
}
// ]]></script>

手机测试了下还不错。


本文共 34 个字数,平均阅读时长 ≈ 1分钟
0

打赏

海报

正在生成.....

评论 (0)

取消