使用cookie显示Modal只显示一次

时间:2012-11-12 22:31:22

标签: jquery modal-dialog

我正在使用(Reveal Modal)并使用以下代码在页面上加载:

$(document).ready(function() {
    $('#myModal').reveal();
});

我怎么能修改这个,我相信使用cookies(还有另外一种方法吗?),这样它只会让用户在一周的时间内弹出一次?

4 个答案:

答案 0 :(得分:17)

使用carhartl的jquery-cookie插件。

在显示模态之前检查cookie。如果它存在,请不要显示它。如果不是,请存储新的cookie并显示模态。

$(document).ready(function() {
    if ($.cookie('modal_shown') == null) {
        $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
        $('#myModal').reveal();
    }
});

答案 1 :(得分:4)

启发mreq的代码我使用基金会5创建了一个仅在3秒后出现的自动加载:

$(document).ready(function() {
  if ($.cookie('modal_shown') == null) {
      $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
      setTimeout(function(){
         $("#myModal").foundation('reveal', 'open');
      }, 3000);
   }
});

是相同的代码,但有3秒的延迟。 :)

答案 2 :(得分:3)

我有一个类似的场景,只显示一个依赖于当周当天的jquery模式。我使用了dateJS插件和jquery cookie插件。

这是我的代码:

$(document).ready(function () {
/* We first check if the cookie is equal to null (doesn't exist), if so, we set the cookie to the current site path */
    if($.cookie('modal') == null){
        // Set the cookie
        $.cookie('modal', 'yes', { path: '/' });

        /* Now that the cookie is set once, load the specials */
        // Load Monday's Specials
        if( Date.today().is().monday() ){
            $('#myModal-Monday').reveal();
        }
        // Load Tuesday's Specials
        else if ( Date.today().is().tuesday() ){
            $('#myModal-Tuesday').reveal();
        }
        else if( Date.today().is().wednesday() ){
            $('#myModal-Wednesday').reveal();
        }
        else if( Date.today().is().thursday() ){
            $('#myModal-Thursday').reveal();
        }
        else if( Date.today().is().friday() ){
            $('#myModal-Friday').reveal();
        }
        else if( Date.today().is().sunday() ){
            $('#myModal-Sunday').reveal();
        }
    }
    // The cookie will delete when the browser-closes. Thus only displaying once on the site until the browser is closed
});

因此,这种模式仅在用户浏览器会话期间显示一次,并仅在用户关闭/重新打开浏览器时重新显示。

关于如何优化的任何反馈都会很棒!

答案 3 :(得分:1)

这就是我用jQuery和普通Js做到的。这里有很好的解释:http://www.w3schools.com/js/js_cookies.asp

//  Set the Cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

// Get the Cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca   = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var  c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if ( c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

// Check if Cookie exists
function checkCookie() {

    // Get the cookie called "visited"
    var visited = getCookie("visited");

    // If it exists, print the Cookie to the Console
    if (visited == "true") {
        console.log(document.cookie);
    }
    else {
        // If not, add the class 'is-visible' to my modal called '.mhc-intro-modal'
        // and create a the cookie "visited=true" expiring in 15 days.
        $('.mhc-intro-modal').addClass('is-visible');
        $('body').addClass('hide-overflow');
        setCookie("visited", "true", 15);
    }
}

// When document is ready check for the cookie
$(document).ready(function() {
    checkCookie();
});

// Close the modal
$('.mhc-intro-modal').on('click', function(event) {
    if ( $(event.target).is('.mhc-intro-modal-close') || $(event.target).is('.mhc-intro-modal') ) {
        event.preventDefault();
        $(this).removeClass('is-visible');
        $('body').removeClass('hide-overflow');
    }
});

CSS

body {
    background-color: #fbfbfb;

    &.hide-overflow {
        overflow: hidden;
    }
}

.mhc-intro-modal {
    position: fixed;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    background-color: rgba(21, 21, 21, 0.55);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s 0s, visibility 0s 0.3s;

    &.is-visible {
        opacity: 1;
        visibility: visible;
        z-index: 11;
        transition: opacity 0.3s 0s, visibility 0s 0s;
    }
}

HTML

<div class="mhc-modal mhc-intro-modal" role="alert">
    <div class="mhc-modal-inner">
        <div class="mhc-modal-info-container">
            <a href="#0" class="mhc-intro-modal-close">Close</a>
            <div class="mhc-copy-container" style="background-image: url('your-image-path.jpg');">
                <h3 class="mhc-title">The Title</h3>
                <p class="mhc-description">The description bla bla bla</p>
            </div>
        </div>
    </div>
</div>