Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

检测页面是在移动端还是PC端打开的方法总结 #18

Open
zlluGitHub opened this issue Apr 21, 2018 · 0 comments
Open

检测页面是在移动端还是PC端打开的方法总结 #18

zlluGitHub opened this issue Apr 21, 2018 · 0 comments

Comments

@zlluGitHub
Copy link
Owner

zlluGitHub commented Apr 21, 2018

  • Navigator对象

    在介绍方法之前,先了解下Navigator 对象,Navigator对象包含有关浏览器的信息,下面的userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。实际上就是利用正则去判断 navigator.useragent 是否含有 Android/webOs/iphone 等字符串,并且利用修饰符 "i" 做了不区分大小写,然后用正则的方法 test 去判断是否满足,或者利用字符串的 indexOf 方法做判断。
  • 方法1:

    function IsPC() {
       var userAgentInfo = navigator.userAgent;
       var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone", "iPad", "iPod"];
       var flag = true;
       for (var i = 0; i < Agents.length; i++) {
              if (userAgentInfo.indexOf(Agents[i]) > 0) {
                   flag = false;
                   break;
              }
       }
       return flag;
     }
     if(IsPC()==true) {
        window.location.href = "http://www.zhenglinglu.cn/pc/"; pc站地址
     } else {
        window.location.href = "http://www.zhenglinglu.cn/move/"; 移动端地址
     }
  • 方法2:

    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.zhenglinglu.cn */
        }
    }
browserRedirect();
  • 方法3:

    function uaredirect(f) {
 try {
 if (document.getElementById("bdmark") != null) {
  return
 }
 var b = false;
 if (arguments[1]) {
  var e = window.location.host;
  var a = window.location.href;
  if (isSubdomain(arguments[1], e) == 1) {
  f = f + "/#m/" + a;
  b = true
  } else {
  if (isSubdomain(arguments[1], e) == 2) {
   f = f + "/#m/" + a;
   b = true
  } else {
   f = a;
   b = false
  }
  }
 } else {
  b = true
 }
 if (b) {
  var c = window.location.hash;
  if (!c.match("fromapp")) {
  if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|SymbianOS)/i))) {
   location.replace(f)
  }
  }
 }
 } catch(d) {}
}
function isSubdomain(c, d) {
 this.getdomain = function(f) {
 var e = f.indexOf("://");
 if (e > 0) {
  var h = f.substr(e + 3)
 } else {
  var h = f
 }
var g = /^www\./;
 if (g.test(h)) {
  h = h.substr(4)
 }
 return h
 };
 if (c == d) {
 return 1
 } else {
 var c = this.getdomain(c);
 var b = this.getdomain(d);
 if (c == b) {
  return 1
 } else {
  c = c.replace(".", "\\.");
  var a = new RegExp("\\." + c + "$");
  if (b.match(a)) {
  return 2
  } else {
  return 0
  }
 }
 }
};

使用方法:
<script type=text/javascript>uaredirect("手机站地址","WEB站地址");</script >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant