0

我有 4-5 个 HTML 页面,我只想显示一次弹出窗口,这意味着弹出窗口应该显示在每个页面中。它工作正常。

但是我希望,当使用关闭图标隐藏模式时,它永远不会在关闭窗口选项卡之前显示。

$(function () {
    if (localStorage.getItem('popState') != 'shown') {
        $("#popup").fadeIn();
        localStorage.setItem('popState', 'shown');
    }

    $('#popup-close').click(function (e) {
        $('#popup').fadeOut();
        localStorage.setItem('popState', 'shown');
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="footer-alert" id="popup" style="display:none;">
  <div class="footer-alert-close" id="popup-close">X</div>
  <p>We use cookies to make interactions with our websites and services easier and more meaningful, including learning to understand how they are used. You can read more about our <a href="privacy.html"><b>Privacy &amp; Cookies Policy here</b></a>. By continuing
    to use this site you are giving us your consent to our policy.</p>
</div>

4

2 回答 2

1

您必须从中删除localStorage.setItem('popState', 'shown');if (localStorage.getItem('popState') != 'shown')因为它your local storage一直在页面加载时设置

做这个

    $(function () {
        if (localStorage.getItem('popState') != 'shown') {
            $("#popup").fadeIn();
        }

        $('#popup-close').click(function (e) {
            $('#popup').fadeOut();
            localStorage.setItem('popState', 'shown');
        });
    });
于 2019-03-29T11:51:46.183 回答
0

我从您的问题中了解到的是,您希望在所有页面上显示弹出窗口,直到关闭。一旦用户关闭它,您就不想在同一会话中再次显示它。

您将需要删除这两行,因为它们会在每次提示弹出窗口的新 html 页面上重置会话存储,即使它已关闭一次):-

 sessionStorage.removeItem('shown');
 sessionStorage.clear();

您甚至可以根据需要尝试 localstorage 或 cookie。

于 2019-03-29T11:29:02.140 回答