我想在从另一个页面加载时清除页面的 cookie,而不是在重新加载同一页面时
I want clear the cookies of a page when loading from another page but not when the same page is reloaded
我正在建立一个新网站,为此我创建了一个页面,该页面在 cookie 中存储了一个数组,我正在使用
清除该数组
$.removeCookie("checkboxValues");
我在运行文档准备好时执行此功能。当我使用 link 从另一个页面转到此页面时,我想清除 cookie,是的,这正在发生。但我的问题是,如果我加载相同的页面(使用 f5 或 ctrl + r),cookie 会被清除,我不想丢失那些 cookie。任何形式的帮助表示赞赏。谢谢。
您可以尝试以下之一:
the easiest way I can think of to achieve this would be to put a
time-stamp in the query string of the redirect and read this in
JavaScript. Set a cookie with this time stamp and detect whether it
already exists to tell whether the page is being reloaded (has been
loaded with the query-string before).
代码:
在有人第一次访问该页面时存储一个 cookie。刷新时检查您的 cookie 是否存在,如果存在,请发出警报。
function checkReload() {
if(document.cookie.indexOf('YOURCOOKIE')==-1) {
// cookie doesn't exist, create it now
}
else {
// not first visit, so alert
alert('You refreshed!');
}
}
并在您的正文标签中:
<body onload="checkReload()">
再来一个:
第一步是检查 sessionStorage 是否有一些预定义的值,如果它存在提醒用户:
if (sessionStorage.getItem("is_reloaded")) alert('Reloaded!');
第二步是将 sessionStorage 设置为某个值(例如 true):
sessionStorage.setItem("is_reloaded", true);
会话值一直保留到页面关闭,因此只有在站点的新选项卡中重新加载页面时它才会起作用。
我正在建立一个新网站,为此我创建了一个页面,该页面在 cookie 中存储了一个数组,我正在使用
清除该数组$.removeCookie("checkboxValues");
我在运行文档准备好时执行此功能。当我使用 link 从另一个页面转到此页面时,我想清除 cookie,是的,这正在发生。但我的问题是,如果我加载相同的页面(使用 f5 或 ctrl + r),cookie 会被清除,我不想丢失那些 cookie。任何形式的帮助表示赞赏。谢谢。
您可以尝试以下之一:
the easiest way I can think of to achieve this would be to put a time-stamp in the query string of the redirect and read this in JavaScript. Set a cookie with this time stamp and detect whether it already exists to tell whether the page is being reloaded (has been loaded with the query-string before).
代码:
在有人第一次访问该页面时存储一个 cookie。刷新时检查您的 cookie 是否存在,如果存在,请发出警报。
function checkReload() {
if(document.cookie.indexOf('YOURCOOKIE')==-1) {
// cookie doesn't exist, create it now
}
else {
// not first visit, so alert
alert('You refreshed!');
}
}
并在您的正文标签中:
<body onload="checkReload()">
再来一个:
第一步是检查 sessionStorage 是否有一些预定义的值,如果它存在提醒用户:
if (sessionStorage.getItem("is_reloaded")) alert('Reloaded!');
第二步是将 sessionStorage 设置为某个值(例如 true):
sessionStorage.setItem("is_reloaded", true);
会话值一直保留到页面关闭,因此只有在站点的新选项卡中重新加载页面时它才会起作用。