开发者

Problem setting a jQuery cookie on page load fancybox popup

开发者 https://www.devze.com 2023-03-16 05:15 出处:网络
I am trying to set the on page load fancybox popup with cookie but it doesn\'t work. There is something I missed, dont see and I need your help. The with-no-cookie script works properly.

I am trying to set the on page load fancybox popup with cookie but it doesn't work. There is something I missed, dont see and I need your help. The with-no-cookie script works properly. I have added the jquery.cookie.js alsoalong with the jquery/1.5.2/jquery.min.js and changed the name of the cookie many times for testing purposes, none of them worked.

This is the onload popup got from the official page

<script>
jQuery(document).ready(function() {
    $.fancybox(
        '<h2>Hi!</h2><p>Lorem ipsum dolor</p>',
        {
                'autoDimensions'    : false,
            'width'             : 350,
            'height'            : 'auto',
            'transitionIn'      : 'none',
            'transitionOut'     : 'none'
        }
    );
});
</script>

This is the onload popup WITH cookie

<script>
$(function() {
    if ($.cookie('mycookie')) {
        // it hasn't been three days yet
    } else {
        $.fancybox(
            '<h2>Hi!</h2><p>Lorem ipsum dolor</p>',
            {
                'autoDimensions'    : false,
                'width'             : 350,
                'height'            : 'auto',
                'transitionIn'      : 'none',
                'transitionOut'     : 'none'
            }
        );
    }
});

// set cookie to expire in 3 days
$.cookie('mycookie', 'true', 开发者_开发问答{ expires: 3});
</script>


This should work. Be sure that you run this on a webserver, browsers may not allow setting local cookies(e.g. Chrome)

But however, if you really like to check the cookie-value and not only if the cookie exists, you can't do it that way. Every cookie-value will be recognized as a boolean true (except null and a empty string)

Use

if ($.cookie('mycookie')==='true') 

instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消