【发布时间】:2015-01-27 01:41:58
【问题描述】:
在我的主页中,我有以下脚本,当用户访问我们的网站时,我们会检查两件事
如果屏幕尺寸小于 800,我们会将它们重定向到移动网站
如果他们来自移动网站,因为他们可以从移动网站的页脚中选择“查看完整网站”,那么我们需要创建一个 cookie 以允许个人浏览该网站而无需任何中断。
现在使用下面的代码,我们遇到了一些问题。当我们这样做时,如下所示
var value = getCookie('example');
如果他们第一次直接浏览该网站并且之前的 url 不是移动网站,我会期望值总是被低估。
但是,如果我直接转到主站点并定向到移动设备并按“查看完整站点”,则以下脚本将运行,但从未创建 cookie,因此当我单击主站点上的菜单项之一时并且浏览回到主页我被重定向到移动网站,因为 cookie 的值被低估了?所以问题是我在这里做错了什么?
澄清cookie是否为空,将他们重定向到移动网站,如果不为空,则不重定向他们,让他们浏览主站点。
var oldURL = document.referrer;
var value = getCookie('example');
alert(oldURL);
alert(value); // Always undefined?
// If this is true they have come from the mobile site
if (oldURL.indexOf("m.domain") > -1) {
if (value == null) {
var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "Yes", { expires: date });
}
}
else { // Otherwise if the screen width is small then 800px wide re-direct them to the mobile site
if (value == null) { // If value is null that means they have come to the main site so re-direct them to the mobile version
if (screen.width <= 800) {
window.location = "m.domain.com";
}
}
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
【问题讨论】:
标签: javascript jquery redirect cookies