views:

276

answers:

3

I've got an animation that I want to play only once and I figured that since the animation was in JQuery, the function to write the cookie file should be as well.

So far I've got:

$(document).ready(function(){

if (!$.cookie('cookieName'))
 {
    setTimeout(function() {    
    $('#intro').fadeOut(1000); 
 }, 5000);
};

$.cookie('cookieName', 'the_value');

});

I guess I was thinking this was going to check to see if there was a cookie and if not then play the animation. When that was done it writes a cookie and so when I come back to the page it doesn't play. I'm calling the Klaus Hartl cookie plugin and for some reason this just isn't doing it for me.

Much appreciated.

A: 

I would download the Web Developer toolbar for Firefox and use it to examine the cookies. Can you see it being made locally?

It could also be, if 'the_value' is something falsy, it won't work. For example, if the value was '0', I think that would evaluate to false.

alex
didnt even know the dev bar had a cookie tool. Cool.Uhh.. no. I dont see it being made locally. Does this mean im not calling the JS plug in correctly?
theHatter
Perhaps not. Make sure your scripts are in this order (top to bottom): jQuery, jQuery Cookie Plugin, your script
alex
My scripts are in the right place if thats the order.Is the syntax off in the If statement?
theHatter
+1  A: 

I took a look at the code for that plugin. I think you might need to specify options and the expiry when you create the cookie. Something like this:

   var date = new Date();
   date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
   $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });

Code ripped off the plugin site :) - http://stilbuero.de/jquery/cookie/

aip.cd.aish
Im beginning to think the problem is with the plugin, but no one else is making a big deal about it so Im inclined to think Im the one whos wrong.In the http://stilbuero.de/jquery/cookie/jquery.cookie.js - documentation for the plugin</a> they say: @example $.cookie('the_cookie', 'the_value');* @desc Set the value of a cookie.Isnt that what Im trying to do (and doing) above?
theHatter
I took a quick look at the source code, I think by default the expiry is set to -1 days, which I believe removes the cookie right away. You should try the above. Just add the first 2 lines before you call $.cookie. And call $.cookie with the third parameter like above.
aip.cd.aish
Yep. works like a charm.Wish they SAID that default was -1 in the docs, but there isnt a lot online about it. Thanks for the eyes.
theHatter
By default, the cookie plugin doesn't set an expiration and the cookie expires at the end of the session. It only sets expiration to -1 if a null value is passed, in order to delete the cookie.
emmychan
but it didnt create a cookie file locally without the var date = new Date(); date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));Maybe I misunderstand a session cookie, but if I dont close the browser then the session should still be open.
theHatter
A: 

try

alert($.cookie('cookieName')); 

and see if it has a value on it.

or

have you tried

if($.cookie('cookieName') == null) { 
// do your thing
// then set cookie and put a great amount of days in the expire param
}
Reigel
The code for if($.cookie('cookieName') == null) worksbut Its still not making the cookie that I can see in the Dev bar when i use $.cookie('cookieName', 'the_value'); to specify cookieName + Method
theHatter