仅显示一次弹出消息

时间:2018-03-21 09:15:50

标签: javascript html laravel

每次用户访问我的网页时都会显示一条弹出消息。这很烦人,我想只显示一次弹出消息。怎么做?



<script>
    $(document).ready(function () {
        $("#popup").hide().fadeIn(1000);
        //close the POPUP if the button with id="close" is clicked
        $("#close").on("click", function (e) {
            e.preventDefault();
            $("#popup").fadeOut(1000);
        });
    });
</script>
&#13;
<!-- Pop up message -->
<link href="myhome/popup.css" rel="stylesheet" type="text/css"/>
<div class="body-popup" style="height: 100%; width: 100%; background-color: black">
    <div id="popup">
        <div id="close" class="alert alert-info alert-dismissable">
            <button type="button" class="pull-right close" data-dismiss="alert" aria-hidden="true">&times;</button>
            CLOSE
            <img src="/myhome/assets/img/prom.jpg" alt="popup" class="image-pop"/>
        </div>
    </div>
</div>
 
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

一旦他们看到它,请将其保存在localStorage中。

$(document).ready(function () {
    const popup = $("#popup");
    popup.hide()
    if (localStorage.seenPopup) return;
    popup.fadeIn(1000);
    //close the POPUP if the button with id="close" is clicked
    $("#close").on("click", function (e) {
        e.preventDefault();
        $("#popup").fadeOut(1000);
        localStorage.seenPopup = 'true'; // value doesn't matter as long as it's not falsey
    });
});

答案 1 :(得分:0)

您可以编写并检查Cookie,如下所示:

...
const showPopup = $.cookie('noPopup');
if (!noPopup) {
   $("#popup").hide().fadeIn(1000);
   $("#close").on("click", function (e) {
        e.preventDefault();
        $("#popup").fadeOut(1000);
        $.cookie('noPopup', true);
    });
}
....