在 Asp.Net 核心 3 网络应用程序中仅显示一次模态

Only display modal one time in Asp.Net Core 3 web app

我更新了我工作的其中一个网站,主页上有一个欢迎模式。我只希望该模式显示 60 天,并且仅在有人第一次浏览该页面时显示。 (显然,它目前会在您移至该页面时显示)

我可以在用户登录后添加一个声明来控制它,但是对于未注册的基本用户如何做到这一点?

正在通过 window onload javascript 事件加载模态。我假设应该在那里进行日期检查?

<script>

    $(window).on('load',function(){

        $('#modal-welcome').modal('show');

    });
    //make sure modal is in center of screen
    function alignModal() {
        var modalDialog = $(this).find(".modal-dialog");
        /* Applying the top margin on modal dialog to align it vertically center */
        modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));
    }
    // Align modal when it is displayed
    $(".modal").on("shown.bs.modal", alignModal);

    // Align modal when user resize the window
    $(window).on("resize", function () {
        $(".modal:visible").each(alignModal);
    });

</script>

这是模态

<!-- Welcome Modal -->
<div id="modal-welcome" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-popout">
        <div class="modal-content">
            <div class="block block-themed block-transparent remove-margin-b">
                <div class="block-header bg-primary-dark">
                    <ul class="block-options">
                        <li>
                          <button data-dismiss="modal" type="button"><i class="si si-close"></i></button>
                        </li>
                    </ul>
                    <h3 class="block-title text-center">Welcome to our updated website!</h3>
                </div>
                <div class="block-content">
                    <partial name="_WelcomePartial" />                    
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-sm btn-default" type="button" data-dismiss="modal">Close</button>
                <button id="modal-welcome-agree" class="btn btn-sm btn-primary" type="button" data-dismiss="modal"><i class="fa fa-check"></i>Surf Away!</button>
            </div>
        </div>
    </div>
</div>
<!-- END Terms Modal -->

您需要在页面上找到任何模式并在每个事件周围放置一个 "if not visible" 使一个出现,像这样:

// assuming "modal" is a class that exists on every modal
if (!$(".modal").is(":visible")) {
  // event that makes a modal appear goes here
}

所以逻辑是这样的,如果没有一个模式已经可见,就让这个可见。

通过使用不同的术语进行一些搜索。现在有 sessionStorage 在 HTML 5 中持续整个浏览会话。这是要走的路。脚本很简单。

 <script>
    var today = new Date();
    //put whatever future date you want below
    var showUntil = new Date("2019-12-31");

    if (sessionStorage.getItem("shown") === null) {
        //show only until the end of the year
        if (today < showUntil) {
            $(window).on('load',function(){
                $('#modal-welcome').modal('show');
                sessionStorage.setItem("shown","true");
            });
        }
    }
 </script>