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