繁体   English   中英

如何在第二次访问WordPress页面时隐藏引导程序模式

[英]how to hide bootstrap modal on second visit of a wordpress page

我正在以Bootstrap为主题的wordpress中的页面。 页面加载上有一个模态窗口,上面有一个注册表单。用户无法提交表单就看不到内容。我的问题是,当evr用户返回该页面时,它会显示模态窗口,我想将其限制为一次给用户。我怎么能做到这一点。

这是我的模态Js:

<script type="text/javascript">
jQuery(document).ready(function($) {
    $('#DownloadSignup').modal('show');
});
</script>

这是我尝试过的方法,但无法正常工作:

<script>
jQuery(document).ready(function($) {
    var isshow = localStorage.getItem('status');
  // alert(isshow);
    if (isshow == null) {
        localStorage.setItem('isshow', 1);

        // Show popup here
        $('#DownloadSignup').modal('show');
    } else if(isshow != null) {
       $('#DownloadSignup').modal('hide');
    }
});
</script>

您需要为此使用缓存会话

如果还希望非登录用户使用它,则应使用缓存。 每次访问站点时,都需要检查缓存是否存在于系统中(如果已设置),则不要显示模式。 这些将在第一次访问时设置,以便在此之后的每次访问都已经存在,从而隐藏了模式。

如果需要登录用户使用它,则应使用会话,遵循与上述相同的逻辑。 在会话中设置一个值,并在显示模式之前检查它是否存在。

改变这个

localStorage.setItem('isshow', 1);

localStorage.setItem('status', 'shown');

在您的页面中使用此代码,希望对您有所帮助。

<script>
    if (!localStorage['someName']) {
        localStorage['someName'] = 'yes';
        myFunction();
    }
</script>

我想如果您检查是否设置了本地存储,则您没有正确检查它,那么您必须检查其类型和值,如下所示...

var = localStorage.getItem('status');
if (localStorage.getItem("isshow") === null) {
   //...
} 

您是否尝试过使用jQuery Cookies。 您可以使用此库https://github.com/carhartl/jquery-cookie

$.cookie("Show", 1, {
   expires : 1,           //expires in 1 day

   path    : '/',          //The value of the path attribute of the cookie 
                           //(default: path of page that created the cookie).

   domain  : 'yourdomain.com',  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});

如果您对JQuery不感兴趣,请查看有关如何使用本机JS进行设置的其他答案。 https://stackoverflow.com/a/1460174/1411055

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM