Twitter Bootstrap Modal和Jquery Cookie插件

时间:2013-05-28 14:50:36

标签: jquery wordpress cookies twitter-bootstrap modal-dialog

我试图让模态每周弹出一次。

我正在使用Wordpress和Roots主题,并将脚本排队并正确注册。

模态的代码是

<div class="modal hide fade modalborder" id="myModal">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <div class="modal-body">
            <h2 class="modalheader">Header Here</h2>
            <p class="modalcoffee">Text here</p>
            <p class="modalcomesin">Text here</p>
        </div>
        <p class="modallearn">Text Here</p>
        <p class="modalready">Are you ready to <span class="modalstart">start</span>?</p>
        <a href="#" class="btn btn-info">Click Here</a>
    </div>
</div>

使这个节目的当前javascript完美无缺:

<script type="text/javascript">
$(window).load(function(){
    $('#myModal').modal('show');
});
</script>

我对jQuery和javascript知之甚少。我需要做些什么来使用cookies?

2 个答案:

答案 0 :(得分:4)

首先,您需要包含JQuery cookie plugin,如下所示:

<script src="/path/to/jquery.cookie.js"></script>

然后以下代码可以帮助您:

<script type="text/javascript">
$(function(){
    // If the cookie does not exist
    if ($.cookie('modalshow') === null) 
    {
       // Show the modal
       $('#myModal').modal('show');
       var expiryDate = new Date();
       var hours = 168;
       expiryDate.setTime(expiryDate.getTime() + (hours * 3600 * 1000));

       // Create cookie to expire in 168 hours (1 week)
       $.cookie("modalshow", "false", { path: '/', expires: expiryDate });
    }
});
</script>

答案 1 :(得分:0)

@mccanff 相同,但不使用外部库

if (document.cookie.indexOf("modalshow=true") < 0) {
    $('#myModal').modal('show');
    var expiryDate = new Date();
    var hours = 168;
    expiryDate.setTime(expiryDate.getTime() + (hours * 3600 * 1000));
    document.cookie = "modalshow=true; expires=" + expiryDate + "; path=/";
}