首页 >web前端 >js教程 >js判断手机访问或者PC的几个例子(常用于手机跳转)_javascript技巧

js判断手机访问或者PC的几个例子(常用于手机跳转)_javascript技巧

WBOY
WBOY原创
2016-05-16 15:25:321127浏览

例子一

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

例子2

<script type="text/javascript">
(function(){
  var reWriteUrl = function(url){
    if(url){
      var Splits = url.split("/"),siteName=window.location.pathname;
      if(typeof siteName!=="undefined"){
        return "http://m.jb51.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("&#63;mobile")<0){
    try{
      window.location.href=reWriteUrl(url);
    }catch(e){}
    }
  };
})();
</script>

注意,如果给搜索引擎看的话我们在文件头加入
e140aaf4842513a1731d4a55df56ada1
0248b59a466ad3042e5462da3ac06bb1
意思是告诉搜索引擎移动蜘蛛抓取的是手机版本的.

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn