Home  >  Article  >  Web Front-end  >  Several examples of js judging mobile phone access or PC (commonly used for mobile phone jumps)_javascript skills

Several examples of js judging mobile phone access or PC (commonly used for mobile phone jumps)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:25:321096browse

Example 1

<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>

Example 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>

Note, if we show it to search engines, we will add
to the file header. e140aaf4842513a1731d4a55df56ada1
0248b59a466ad3042e5462da3ac06bb1
It means telling the search engine mobile spider to crawl the mobile version.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn