zoukankan      html  css  js  c++  java
  • JS判断是手机访问还是PC端访问网站

    <script>
        if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) {
            location.replace("http://m.helloweba.com")
        }else{
            document.write("请使用手机访问.");
        }
    </script>

    百度的判断代码

    复制代码 代码如下:

    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>

  • 相关阅读:
    HDU 5642 King's Order 动态规划
    HDU 5640 King's Cake GCD
    HDU 5641 King's Phone 模拟
    HDU 5299 Circles Game 博弈论 暴力
    HDU 5294 Tricks Device 网络流 最短路
    HDU 5289 Assignment rmq
    HDU 5288 OO’s Sequence 水题
    星际争霸 虚空之遗 人族5BB 操作流程
    Codeforces Beta Round #3 D. Least Cost Bracket Sequence 优先队列
    Codeforces Beta Round #3 C. Tic-tac-toe 模拟题
  • 原文地址:https://www.cnblogs.com/sweet521/p/5684363.html
Copyright © 2011-2022 走看看