【发布时间】:2014-02-04 00:15:05
【问题描述】:
我创建了一个简单的脚本来检测用户设备,如果该设备是移动设备,它将重定向到移动端。当您在移动设备上时,该网站将创建一个 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: '/'});
有什么想法吗?
【问题讨论】:
标签: javascript jquery wordpress cookies