[英]How Do You Break A JavaScript Redirect To Mobile?
I have created a simple script that will detect the users device and if that device is mobile than it will redirect to the mobile side. 我创建了一个简单的脚本,它将检测用户设备,并且如果该设备是移动设备,它将重定向到移动设备。 When you are on mobile the site will create a cookie that should essentially break the redirect when you visit desktop again. 当您在移动设备上时,该站点将创建一个cookie,当您再次访问台式机时,该cookie实际上会中断重定向。 Below is the code on the main website: 以下是主网站上的代码:
<script>
// simple mobile detection script
// create a variable to store response to user agent queries
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
// if any of the listed agents are accessing the page, redirect to mobile version with geolocation enabled
if(isMobile.any())
{
if(jQuery.cookie('mobilea')!== 1)
{
window.location="/mobile/";
console.log('true');
}
}
</script>
The cookie is successfully being instantiated on the mobile side with the following code: 使用以下代码在移动端成功实例化了cookie:
jQuery.cookie('mobilea',1, {path: '/'});
Any ideas? 有任何想法吗?
if(jQuery.cookie('mobilea')!== 1)
will always be false because '1'
is not exactly equal to 1
. if(jQuery.cookie('mobilea')!== 1)
将始终为false,因为'1'
不完全等于1
。 Either switch to !=
, or replace 1
with '1'
. 切换到!=
,或将1
替换为'1'
。
if(jQuery.cookie('mobilea')!== '1')
jQuery.cookie('mobilea')
is going to return a string and not an integer, so your strict check on it will never be false. jQuery.cookie('mobilea')
将返回一个字符串而不是整数,因此严格检查它永远不会是错误的。 If you change it to 如果您将其更改为
jQuery.cookie('mobilea') !== '1'
You can test it properly 您可以正确测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.