Home >Web Front-end >JS Tutorial >js solution to keep refreshing when judging whether it is PC or mobile
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
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!