Simple Javascript/JQuery Cookie Question
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.
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/
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.
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
}
精彩评论