如果 cookie 为空,则发出设置和重定向用户 jquery
Issue setting and redirecting user if cookie is null jquery
在我的主页上有以下脚本,当用户访问我们的网站时,我们会检查两件事
如果屏幕尺寸小于 800,我们会将他们重定向到移动网站
如果他们来自移动网站,因为他们可以选择从移动网站的页脚 "View Full Site",那么我们需要创建一个 cookie 以允许个人不受任何干扰地浏览网站。
现在使用下面的代码我们遇到了一些问题。当我们做
时如下所示
var value = getCookie('example');
如果他们第一次直接浏览该网站并且之前的 url 不是移动网站,我会预料到价值总是不足。
但是如果我直接转到主站点并转到移动站点并按 "View Full Site" 下面的脚本将 运行 但是永远不会创建 cookie 所以当我点击其中一个主站点上的菜单项和浏览回主页我被重定向到移动站点,因为 cookie 的值是 underfined?所以问题是我在这里做错了什么?
澄清 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);
}
}
}
好的,设法让它与下面的一起工作。
var oldURL = document.referrer;
var t = getCookie("fromMobile");
if (oldURL.indexOf("m.domain") > -1) {
var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
document.cookie = "fromMobile=Yes; expires="+ date.toGMTString() +"; path=/";
} else {
if (t == "") {
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
}
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
在我的主页上有以下脚本,当用户访问我们的网站时,我们会检查两件事
如果屏幕尺寸小于 800,我们会将他们重定向到移动网站
如果他们来自移动网站,因为他们可以选择从移动网站的页脚 "View Full Site",那么我们需要创建一个 cookie 以允许个人不受任何干扰地浏览网站。
现在使用下面的代码我们遇到了一些问题。当我们做
时如下所示var value = getCookie('example');
如果他们第一次直接浏览该网站并且之前的 url 不是移动网站,我会预料到价值总是不足。
但是如果我直接转到主站点并转到移动站点并按 "View Full Site" 下面的脚本将 运行 但是永远不会创建 cookie 所以当我点击其中一个主站点上的菜单项和浏览回主页我被重定向到移动站点,因为 cookie 的值是 underfined?所以问题是我在这里做错了什么?
澄清 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);
}
}
}
好的,设法让它与下面的一起工作。
var oldURL = document.referrer;
var t = getCookie("fromMobile");
if (oldURL.indexOf("m.domain") > -1) {
var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
document.cookie = "fromMobile=Yes; expires="+ date.toGMTString() +"; path=/";
} else {
if (t == "") {
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
}
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}