简体   繁体   中英

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:

//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 = 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM