简体   繁体   中英

Show Hidden Content with jQuery Cookie

I'm trying to get a div to display after x time for new visitors, and display immediately for returning visitors. I can't figure out why this won't work and hoping someone can point out the error.

Using jQuery Cookie Plugin: https://github.com/carhartl/jquery-cookie

//Make Hidden Content Visible       
        jQuery(document).ready(function($) {

            // set the delay with the following vars
            var hour = 0;
            var minutes = 0;
            var secs = 7;
            var thisdelay = (hour*60*60*1000) + (minutes * 60 * 1000) + (secs * 1000);

            //checks a cookie value
            //If no cookie found, add a cookie for the next visit
            if($.cookie('returningvisitor') === null) {
                var duration = 1; // days until cookie expires
                $.cookie('returningvisitor', 'true', { expires: duration});

                //then wait until delay to display 
                $(".hideshow").delay(thisdelay).fadeIn("fast");
            }
            else {
                //immediately display the content
                $(".hideshow").css("display", "block");

            }
        });

and...

<div class="hideshow" style="display:none;"><p>Hello World</p></div>

And the JS Fiddle... http://jsfiddle.net/mp2E8/1/

Thanks!

According the documentation:

$.cookie('not_existing'); // => undefined

so in your:

$.cookie('returningvisitor') === null

should be:

$.cookie('returningvisitor') === undefined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM