我使用jquery cookies插件,以便在用户每次访问页面时增加cookie的值。我这样做是为了在第一次访问时显示一些东西,然后在第二次访问时显示一些不同的东西,然后再不显示任何东西。
所以我需要确定这是不是用户的第一次访问,第二次访问,以及之后的所有访问。
var cookieTime = jQuery.cookie('shownDialog');
cookie_value = parseInt(cookieTime);
if (cookieTime != 'true') {
jQuery.cookie('shownDialog', '1', 'true', {expires: 7});
cookie_value ++;
}
else if (cookieTime == 'true' && cookie_value > 0){
cookie_value ++;
}每次刷新页面时,我使用的这段代码都会重置。而不是将值保存在cookie中。我不确定保留cookie的值并在每次页面刷新时递增cookie的最佳方法是什么?
发布于 2012-10-24 22:07:22
我不认为
jQuery.cookie('shownDialog', '1', 'true', {expires: 7});是有效的表单。它应该是
jQuery.cookie(cookiename, cookieval, extra);来源:https://github.com/carhartl/jquery-cookie
如果您想检查是否设置了cookie,请检查它是否为空。
// Check if the cookie exists.
if (jQuery.cookie('shownDialog') == null) {
// If the cookie doesn't exist, save the cookie with the value of 1
jQuery.cookie('shownDialog', '1', {expires: 7});
} else {
// If the cookie exists, take the value
var cookie_value = jQuery.cookie('shownDialog');
// Convert the value to an int to make sure
cookie_value = parseInt(cookie_value);
// Add 1 to the cookie_value
cookie_value++;
// Or make a pretty one liner
// cookie_value = parseInt(jQuery.cookie('shownDialog')) + 1;
// Save the incremented value to the cookie
jQuery.cookie('shownDialog', cookie_value, {expires: 7});
}https://stackoverflow.com/questions/13050523
复制相似问题