jquery cookie plugin - can't get it working?
I have a topbar with some important info for my users on my website (just like stackoverflow lets you know when you reached a new badge you see this yellow bar on top that can be closed).
I simply want to have have that bar visible when a visitor comes to my site. Once he closes the bar a cookie should be saved and the bar should never appear again.
Therefore, I am using the jquery cookie plugin. Any ideas why that doesn't work?
var topInfo = $.cookie('topinfo');
if (topInfo) {
$('#topBar').hide();
} else {
$('#topBar').show();
}
$('#topBar .topBarCloser').click(function() {
$(this).parent().slideUp({
duration: 300,
easing: 'easeOutQuint',
complete: function() {
$(this).remove();
$.cookie('topinfo', true, { expires: 1000 开发者_如何学JAVA});
}
});
});
What am I doing wrong here? I've set the expiration date to 1000 days.
Another reason why $.cookie could not be working (jquery.cookie doesn't delete a cookie) - maybe not in this case, but in general may someone look at this page and need this information - is because you set it for a specific path but you're trying to delete it without setting the path again.
Example:
if you set the cookie like this
$.cookie("CookieName", "yourValue", { path: '/' });
and you try to delete it this way:
$.cookie("CookieName",null);
that will not work, you must write:
$.cookie("CookieName", null, { path: '/'});
or wathever path you choose before.
I don't know which cookie plugin you're using, but if it's this one: http://plugins.jquery.com/files/jquery.cookie.js.txt
Then it says:
@param String name The name of the cookie.
@param String value The value of the cookie.
So value has to be a string - you've set it to boolean. This would probably mean that topInfo will always be true (because false as a string will be "false", which is true!) Try setting the cookie to a string value:
$.cookie('topinfo', "on", { expires: 1000 });
// and then...
if (topInfo === "on") {
...
}
精彩评论