JS根据设备判断跳转的另一种处理方式

发布时间:2016-08-04 09:58:53编辑:丝画阁阅读(374)

先判断路径中是否有一个特定的字符.

$(function () {
    if (window.location.href.indexOf("?mobile") >= 0) {
        $.cookie('wapToPc', 'wapToPc', {expires: 1});
    }


如果这个字符存,则判断设备是手机还是PC,再进行相对应的页面跳转.

<script type="text/javascript">
    function includeSharp(){
      var dirs = ['man','woman','game','condom','baojian','runhuaye','ranrenziwei','nvrenziwei','qingqu','neiyi','topic','goods-','help','article'];
      var url = location.href;
      var path = location.pathname;
      var w = "http://www.homeforexchange.com/";
      for (i in dirs){
        if(url.indexOf(w + dirs[i]) == 0){
          window.location.href = "http://wap.homeforexchange.com" + path;
          return;
        }
      }
      window.location.href="http://wap.homeforexchange.com/";
    }
    if (document.cookie.indexOf("wapToPc") &lt; 0){
      if(/android|b(b\d+|lackberry|ada\/)|m(eego|obile|QQBrowser|tk)|ma(te|ui|xo)|ip(hone|ad)|uc(web|Browser)|UNTRUSTED|H(UAWEI|aier|d\/|s\-)|j(2me|ava|uc)|Samsung|oppo|opera m(ob|in)i|play(book|station)|KTOUCH|nokia|w(ap|indows CE)|kindle|lenovo|xiaomi|s(ony|MART-TV)|zte\-|3gpp-gba|pad|ht(c(\-| |_|a|g|p|s|t)|tp)|Linux; U;/i.test(navigator.userAgent)){
        if(window.location.href.indexOf("?mobile")&lt;0){
          try{
            includeSharp();
          }catch(e){}
        }
      }
    }
</script>

关键字