Gregg
Gregg

Reputation: 495

Need help setting a cookie in jQuery

Sorry if this question sounds stupid, but I only know HTML/CSS and so I'm trying to figure things out as best I can.

I'm using jQuery to create a slide down affect when someone scrolls down on my page.

Example: http://buckinvestor.com/test/jquerytest.html

I figured it all out, including a "Close" button, but now I have to figure out how to set a cookie so users don't keep getting the slide down on every page. I'd like to set the cookie for 10 days.

I read that I should be using:

http://plugins.jquery.com/project/cookie

and then to write a cookie do $.cookie("test", 1);
to access the set cookie do $.cookie("test");

But I don't know where I'm supposed to put this code. Can someone please show me the exact code I need to put in place and where? And also, is there anything bad I should be aware of when setting a cookie like this?

Thank you StackOverflow Gods!

You have been a life saver.

Upvotes: 1

Views: 228

Answers (1)

kojiro
kojiro

Reputation: 77167

I imagine you'd want to do something like this:

$(function() {
    var bar = $('#headerSlideContainer'),
        top = bar.css('top');
    if (!($.cookie("test"))) {
        $(window).bind("scroll", function() {
            if($(this).scrollTop() > 50) {
                bar.stop().animate({'top' : '0px'}, 500);
            } else {
                bar.stop().animate({'top' : top}, 500);
            }
        });
        $.cookie("test", 1);
    }
});

Upvotes: 1

Related Questions