开发者

Problem on setting a cookie on a jquery script

开发者 https://www.devze.com 2023-03-16 16:56 出处:网络
can anyone please tell me what is the error on this code? I am trying to set a cookie for the fancybox popup but it is showing on every refresh. All .js are included.

can anyone please tell me what is the error on this code? I am trying to set a cookie for the fancybox popup but it is showing on every refresh. All .js are included.

<script>

$(document).ready(function(){

if(!$.cookie('the_cookie1')){
    $.cookie('the_cookie1', 'true', { expires: 3});

$.fancybox(
        '<h2>Hi!</h2><p>Lorem ipsum dolor</p>',
        {
                'autoDimensions'    : false,
            'width'             : 350,
            'height'            : 'auto',
开发者_Python百科            'transitionIn'      : 'none',
            'transitionOut'     : 'none'
        }
           );
}

});

</script>


I modified the code block to the following and it's functioning as expected both in Chrome 13 and FF5.

<script>

    $(document).ready(function () {
        var cookieName = 'the_cookie1';

        var cookie = $.cookie(cookieName);
        if(cookie === null) {

            var cookieOptions = { expires: 3, path: '/' /*domain:, secure: false */ };

            $.cookie(cookieName, 'true', cookieOptions);

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

    });

</script>

FireFox 5

Problem on setting a cookie on a jquery script

Chrome 13

Problem on setting a cookie on a jquery script

First run, I receive the box and the cookie is set. Thereafter, no box.

I did notice some strangeness with Chrome where the cookie would not appear in the console occasionally but debugging the script in fact revealed the cookie was set and functioning.


try changing your condition to this:

if(! ($.cookie('the_cookie1')){...

this both covers null and 'undefined'.

0

精彩评论

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