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

2018-07-09  本文已影响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;
}
上一篇下一篇

猜你喜欢

热点阅读