跨页面持久化 jQuery Cookie



我正在使用jQuery Cookie插件(https://github.com/carhartl/jquery-cookie(,并且在我网站上的不同页面上保留cookie时遇到问题。

我有一段内容显示在每个页面上,一旦隐藏,它就会像这样设置 cookie(为所有变量道歉,它们根本不相关:

// Binds the close event to the button.
$alert.on('click', function(e) {
    $alertWrap.fadeOut();
    // Sets the breaking-delete cookie to yes.
    $.cookie('breaking-bar-delete', 'yes', {expires: 7 });
});

当初始脚本触发时,它会检查 cookie 是否存在:

// If the current bar is not supressed and they are not in the editor, and they do not have a closed cookie it will setup the bar.
if ($.cookie('breaking-bar-delete') == undefined) {
  $alert.css("display","block");
}
// If there's no news, or they have the closed cookie for the current bar it hides it by default.
if ($.cookie('breaking-bar-delete') == 'yes') {
  $alert.parent().css("display","none");
}

现在,这适用于您隐藏栏的路径,因此在您刷新时它不会重新显示。但是,如果您使用不同的路径转到网站的一部分,它不会检测到cookie并显示内容。

当我最初提供cookie时,是否可以设置某种配置,以便它持续存在于我网站上的所有页面中?

路径:-

路径

路径:"/" 定义 Cookie 有效的路径。

默认情况下,Cookie 的路径

是 Cookie 所在页面的路径已创建(标准浏览器行为(。

如果要使其可用例如,在整个域中使用路径:"/"。

默认值:创建 Cookie 的页面路径。

所以,对于你的饼干:-

$.cookie('breaking-bar-delete', 'yes', { expires: 7, path: '/' });

最新更新