[英]Browser localStorage refreshing at every page
I have coded a pop-up div which I want to display once per visit to the website per user, for which I was able to piece together as follows, but unfortunately it seems to pop-up at every single page I visit on the website: 我已经编码了一个弹出div,我希望每个用户每次访问该网站时都显示一次,为此,我可以将其汇总如下,但不幸的是,它似乎在我访问该网站的每个页面上都会弹出:
//COOKIE POLICY POP-UP
$(document).ready(function () {
if (localStorage.getItem('pops') != 'visible') {
setTimeout(function () {
$('#cookie_popup').css('bottom', '0');
}, 1000);
$('#cookie_popup a.button').click(function () {
$('#cookie_popup').css('bottom', '-100px');
});
localStorage.setItem('pops', 'visible');
}
});
window.onbeforeunload = function () {
localStorage.removeItem('pops');
};
Edit: As pointed on the comments below, the last section window.onbeforeunload needed to be removed for it to keep the cache for the whole session. 编辑:正如下面的评论所指出的那样,需要删除最后一部分window.onbeforeunload,以保留整个会话的缓存。
window.onbeforeunload = function () { localStorage.removeItem('pops');};
is removing the flag so that every time you come to the page, the flag isn't there and the popup is shown. 正在删除该标记,以便每次您进入页面时,该标记都不存在,并显示弹出窗口。 Remove that part and make sure you manually clear
localStorage
yourself before each of your tests. 删除该部分,并确保您在每次测试之前自己手动清除
localStorage
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.