美文网首页
JS根据设备切换PC/mobile域名

JS根据设备切换PC/mobile域名

作者: 阿鲁提尔 | 来源:发表于2018-07-09 13:07 被阅读0次

    pc跳移动

    function checkDevice() {
      var flag = "";
      var agent = navigator.userAgent.toLowerCase();
      var ipadKeywords = ["ipad"];
      var mobileKeywords = ["android", "iphone", "windows phone"];
    
      //排除 Windows 桌面系统
      if (
        !(agent.indexOf("windows nt") > -1) ||
        (agent.indexOf("windows nt") > -1 &&
          agent.indexOf("compatible; msie 9.0;") > -1)
      ) {
        //排除苹果桌面系统
        if (
          !(agent.indexOf("windows nt") > -1) &&
          !(agent.indexOf("macintosh") > -1)
        ) {
          for (var item in ipadKeywords) {
            if (agent.indexOf(ipadKeywords[item]) > -1) {
              flag = "ipad";
              break;
            }
          }
          for (var item in mobileKeywords) {
            if (agent.indexOf(mobileKeywords[item]) > -1) {
              flag = "mobile";
              break;
            }
          }
        }
      }
      return flag;
    }
      var device = checkDevice();
      var isIPad = device == "ipad";
      var isMobile = device == "mobile";
      var hmsr = "";
      var href = window.location.host;
      var pathname = window.location.pathname;
      var search = window.location.search;
      var loc=pathname.substring(1);
      console.log(loc);
      if (isMobile) {
          window.location.href = window.location.href.replace("www", "m");
       //window.location.href = "https://m.dnatime.com/"+loc;
      } else {
        
    }
    

    移动跳pc

    function checkDevice() {
      var flag = "";
      var agent = navigator.userAgent.toLowerCase();
      var ipadKeywords = ["ipad"];
      var mobileKeywords = ["android", "iphone", "windows phone"];
    
      //排除 Windows 桌面系统
      if (
        !(agent.indexOf("windows nt") > -1) ||
        (agent.indexOf("windows nt") > -1 &&
          agent.indexOf("compatible; msie 9.0;") > -1)
      ) {
        //排除苹果桌面系统
        if (
          !(agent.indexOf("windows nt") > -1) &&
          !(agent.indexOf("macintosh") > -1)
        ) {
          for (var item in ipadKeywords) {
            if (agent.indexOf(ipadKeywords[item]) > -1) {
              flag = "ipad";
              break;
            }
          }
          for (var item in mobileKeywords) {
            if (agent.indexOf(mobileKeywords[item]) > -1) {
              flag = "mobile";
              break;
            }
          }
        }
      }
      return flag;
    }
      var device = checkDevice();
      var isIPad = device == "ipad";
      var isMobile = device == "mobile";
      var hmsr = "";
      var href = window.location.host;
      var pathname = window.location.pathname;
      var search = window.location.search;
      var loc=pathname.substring(0);
      console.log(loc);
      if (isMobile) {
       
      } else {
          window.location.href = window.location.href.replace("m.", "www.");
       // window.location.href = "https://www.dnatime.com/"+loc;
    }
    

    相关文章

      网友评论

          本文标题:JS根据设备切换PC/mobile域名

          本文链接:https://www.haomeiwen.com/subject/mklzuftx.html