Home  >  Article  >  Web Front-end  >  js solution to keep refreshing when judging whether it is PC or mobile

js solution to keep refreshing when judging whether it is PC or mobile

不言
不言Original
2018-08-17 14:24:282649browse

The content of this article is about the solution to keep refreshing when js determines whether it is a PC or a mobile terminal. The article introduces the understanding of this in js. It has certain reference value. Friends in need can For reference, I hope it will be helpful to you.

When I was writing the official website, I made both PC and mobile terminals. I searched for such code on the Internet, and it seemed that there was no problem at all. After putting it into the page, the PC page remained unchanged. I refreshed and couldn't stop it at all. I found similar js and still had the same problem. After repeated attempts, I discovered that the problem was an extra line of code:

Original code example:

<script type="text/javascript">
  //平台、设备和操作系统
  var system = {
  win: false,
  mac: false,
  xll: false,
  ipad:false
  };
  //检测平台
  var p = navigator.platform;
  system.win = p.indexOf("Win") == 0;
  system.mac = p.indexOf("Mac") == 0;
  system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
  system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false;
  //跳转语句,如果是手机访问就自动跳转到" "里的页面
  if (system.win || system.mac || system.xll||system.ipad) {
  window.location.href = "  PC端网站名 ";
  } else {
  window.location.href = " 移动端端网站名   ";
  }
  </script>
  
  

Modified code example:

<script type="text/javascript">
  //平台、设备和操作系统
  var system = {
  win: false,
  mac: false,
  xll: false,
  ipad:false
  };
  //检测平台
  var p = navigator.platform;
  system.win = p.indexOf("Win") == 0;
  system.mac = p.indexOf("Mac") == 0;
  system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
  system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false;
  //跳转语句,如果是手机访问就自动跳转到" "里的页面
  if (system.win || system.mac || system.xll||system.ipad) {
 //window.location.href = "PC端网站名";(在PC端引入js代码段时,这一行不能要,否则一直循环刷新,移动端同理)
  } else {
  
  window.location.href = " 移动端端网站名   ";
  }
  </script>

Related recommendations:

js determines whether the user is on PC or mobile

jsHow to determine whether the user is on PC Terminal or mobile terminal access_javascript skills

The above is the detailed content of js solution to keep refreshing when judging whether it is PC or mobile. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn