gpt4 book ai didi

javascript - 跨页面持久保存 jQuery Cookie

转载 作者:行者123 更新时间:2023-11-29 18:00:21 25 4
gpt4 key购买 nike

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

最佳答案

Path :-

path

path: '/' Define the path where the cookie is valid.

By default the path of the cookie is the path of the page where the cookie wascreated (standard browser behavior).

If you want to make it availablefor instance across the entire domain use path: '/'.

Default: path of page where the cookie was created.

因此,对于您的 cookie:-

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

关于javascript - 跨页面持久保存 jQuery Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com