我在写官网的时候做了pc和移动端两个,在通过网上查找了这样的代码,看着完全没问题,等放进去页面中后,pc端页面一直刷新,根本停不下来,找了类似js还是同样的问题。通过不断尝试后才发现,问题就是多了一行代码:
原代码例子:
<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>
修改后代码例子:
<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>
相关推荐:
js判断是pc端还是移动端
js如何判断用户是在pc端和还是移动端访问_javascript技巧
以上就是js判断是pc端还是移动端时一直刷新的解决方法的详细内容。
