简体   繁体   English

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

[英]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.

相关问题 如何使用javascript检测移动设备? - How do you detect a mobile device with javascript? 如何将 javascript 事件监听器用于移动导航菜单? - How do you use javascript event listeners for mobile navigation menus? javascript:移动设备上的插页式广告-您如何处理缩放? - javascript: interstitial ads on mobile - how do you handle zoom? 一旦用户启用了他们的javascript,你如何重定向到一个页面? - how do you redirect to a page as soon as a user enables their javascript? 如何在 javascript 中重定向自定义生成的 URL? - How do you redirect a custom generated URL in javascript? 如何在客户端使用 Javascript 检测页面是否是重定向 - How do you detect if the page is a redirect using Javascript in the client side 您如何知道您使用的JavaScript库在升级后是否会破坏您的代码? - How do you know if a JavaScript library you are using will break your code after an upgrade? 如何防止javascript重定向退出全屏模式? - How do you prevent a javascript redirect from exiting fullscreen mode? 如何使用php而不是javascript重定向到弹出窗口? - How do you redirect to a popup using php and not javascript? 如何在javascript中另一个函数内的循环函数中完全中断/返回? - How do you break/return completely out of a looping function inside another function in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM