javascript - 在 Asp.Net Core 3 Web 应用程序中仅显示一次模式

标签 javascript bootstrap-modal asp.net-core-3.0

我更新了我工作的一个网站,我们的主页上有一个欢迎模式。我只希望该模式显示 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 -->

最佳答案

通过使用不同的术语进行一些搜索。现在,sessionStorage 可以在 HTML 5 中持续整个浏览 session 。这就是正确的方法。脚本很简单。

 <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>

关于javascript - 在 Asp.Net Core 3 Web 应用程序中仅显示一次模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530428/

相关文章:

javascript - 使用 foreach 在数据库 laravel javascript 中双重输入每个输入类型文本

javascript - 如何在模态打开时隐藏弹出窗口

c# - 模型绑定(bind)忽略具有 JsonIgnore 属性的属性

c# - 如何在 ApplicationUserManager 上使用 Identity 2.2 并且不使用 ILookupNormalizer

xml - net core 3 AddXmlSerializerFormatters() 配置选项

javascript - Yargs .check() 用于特定条件

javascript - Express JS 中的 Jade 不是渲染

javascript - 将谷歌地图自动填充地址API插入mysql数据库

javascript - Mapbox GL JS Canvas 在 Bootstrap 模式中无法正确显示

php - 数据库检查后模态不显示