【问题标题】:Persisting jQuery Cookies Across Pages跨页面持久化 jQuery Cookie
【发布时间】:2016-05-27 19:45:33
【问题描述】:

我正在使用 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 时是否可以设置某种配置,以便它在我网站上的所有页面中持续存在?

【问题讨论】:

标签: javascript jquery cookies


【解决方案1】:

Path:-

路径

path: '/' 定义cookie有效的路径。

默认情况下,cookie 的路径是 cookie 所在页面的路径 已创建(标准浏览器行为)。

如果你想让它可用 例如跨整个域使用路径:'/'。

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

所以,对于你的 cookie:-

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

【讨论】:

  • 你,没看到那个。谢谢!
猜你喜欢
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-24
  • 2012-08-09
相关资源
最近更新 更多