浏览器localStorage在每个页面刷新

时间:2018-04-18 16:42:44

标签: javascript jquery browser-cache

我已经编写了一个弹出式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');
};

编辑:正如下面的评论中指出的那样,需要删除最后一部分window.onbeforeunload,以便为整个会话保留缓存。

1 个答案:

答案 0 :(得分:0)

 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.