jQuery Cookies的问题

时间:2016-06-21 20:27:46

标签: jquery cookies

我是创建cookie的新手,需要为jquery弹出窗口创建一个。

Cookie_name = newsletter_displayed - 值应为日期,到期1个月 - 如果设置了newsletter_displayed,则在4分钟后才会显示。

我将时间设置为3和5秒以进行测试。当我点击x时,cookie被设置并且它可以工作。但是,当我刷新页面时,弹出窗口再次出现。如何在弹出窗口隐藏cookie存在的地方?

我的HTML:

<div id="slide">    
    <div id="dialog">
        <div id="close">x</div>
        <div id="wrapper">
            <p class="text-center">SUBSCRIBE TO OUR NEWSLETTER TO RECEIVE EXCLUSIVE DISCOUNTS AND INDUSTRY NEWS.</p>
            <div class="input-group"> 
                <input name="email" type="email" id="newsletter-email" class="required-entry validate-email input-text form-control" placeholder="Enter your email address..."> 
            </div> 
            <span class="input-group-btn"><button class="submit-btn form-control text-center" type="submit">Subscribe <span class="fa fa-envelope"></span></button></span>
        </div>
    </div>
</div>

我的JS:

jQuery(document).ready(function(){
jQuery(function() {
    jQuery("#slide").delay(3000).animate({width:'toggle'}, 750);
    });
jQuery('#close').on('click', function() {

        var $this = jQuery(this);
        var slide = jQuery('#slide');
        if ($this.hasClass('clicked-once')) {
        // already been clicked once, hide it
        createCookie('newsletter_suppressed', 'new Date()', 30);
        if(createCookie('newsletter_displayed', 'new Date()', 30) == 'undefined') {
        createCookie('newsletter_displayed', 'new Date()', 30);
        } else {
        slide.hide();
        }
    } else {
        // first time this is clicked, mark it
        createCookie('newsletter_displayed', 'new Date()', 30);
        $this.addClass('clicked-once');
        jQuery(slide).fadeOut();
        jQuery(slide).delay(5000).animate({width:'toggle'}, 750);
    }    

});

});

1 个答案:

答案 0 :(得分:0)

以下是答案:

jQuery(document).ready(function() {
if(!readCookie('newsletter_suppressed')) {
    jQuery("#slide").delay(3000).animate({width:'toggle'}, 750);
}
jQuery('#close').on('click', function() {

    var $this = jQuery(this);
    var slide = jQuery('#slide');

    if ($this.hasClass('clicked-once')) {
        // already been clicked once, hide it
        createCookie('newsletter_suppressed', new Date(), 30);
        slide.hide();

    } else {
        // first time this is clicked, mark it
        createCookie('newsletter_displayed', new Date(), 30);
        $this.addClass('clicked-once');
        jQuery(slide).fadeOut();
        jQuery(slide).delay(5000).animate({width:'toggle'}, 750);
    }    
});

});