繁体   English   中英

如何破坏JavaScript重定向到移动设备?

[英]How Do You Break A JavaScript Redirect To Mobile?

我创建了一个简单的脚本,它将检测用户设备,并且如果该设备是移动设备,它将重定向到移动设备。 当您在移动设备上时,该站点将创建一个cookie,当您再次访问台式机时,该cookie实际上会中断重定向。 以下是主网站上的代码:

<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>

使用以下代码在移动端成功实例化了cookie:

jQuery.cookie('mobilea',1, {path: '/'});

有任何想法吗?

if(jQuery.cookie('mobilea')!== 1)将始终为false,因为'1'不完全等于1 切换到!= ,或将1替换为'1'

if(jQuery.cookie('mobilea')!== '1')

jQuery.cookie('mobilea')将返回一个字符串而不是整数,因此严格检查它永远不会是错误的。 如果您将其更改为

jQuery.cookie('mobilea') !== '1'

您可以正确测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM