简体   繁体   English

浏览器localStorage在每个页面刷新

[英]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.

相关问题 localstorage 刷新页面保存 - localstorage refreshing page save localStorage是否导致页面无限刷新? - localStorage causes infinite refreshing the page? 使用 Javascript,我试图将数据保存在浏览器的 SessionStorage 和 localStorage 中,但刷新页面后数据丢失 - Using Javascript, I am trying to persist the data in SessionStorage and localStorage of browser but after refreshing the page the data is lost 刷新页面后 localStorage 值设置为 undefined - localStorage values are set to undefined after refreshing the page 刷新页面后LocalStorage不显示todolist数组 - LocalStorage not displaying array of todolist after refreshing page 在不刷新页面的情况下显示更改的 localStorage? - Show changed localStorage without refreshing page? localStorage中的JWT仅在刷新页面后才能工作 - JWT in localStorage only works after refreshing the page 如何在刷新浏览器不更改值(JavaScript)的localStorage中保存? - How to save in localStorage that refreshing browser do not change the value (JavaScript)? Redux、localStorage 和 React:刷新页面时出错 - Redux, localStorage, and React: Getting an error when refreshing my page LocalStorage JS - 刷新页面后存储数据以保持持久性是否安全? - LocalStorage JS - is it safe to store data to keep persistent after refreshing the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM