Show/hide using jQuery cookies
So I've created a simple tip box that fades in on page load, with an option to close the box. I'm trying to make the box hidden if the visitor clicks the close link. I'm fairly new to cookies, so I'm probably doing it all wrong, but this is what I have:
$('#close').click(function(e) {
jQuery.cook开发者_如何学Pythonie('tip', 'hide', cookieOpts);
$(this).parent('div.tip').fadeOut(1000);
e.preventDefault();
});
jQuery.cookie('tip', 'show', cookieOpts);
$('.tip').delay(500).fadeIn(1000);
var shouldShow = jQuery.cookie('tip') == 'show';
var cookieOpts = {expires: 7, path: '/'};
if( shouldShow ) {
$('.tip').delay(500).fadeIn(1000);
}
else {
$('.tip').css('display', 'none');
}
I revised the code:
http://jsbin.com/ujixi4/4/edit
A little bit simpler, hopefully achieves what you want.
var cookieOpts = {expires: 7, path: '/'}; //this isnt working for some reason
alert('c: '+$.cookie('tip')); //debug code
if( $.cookie('tip') == 'hide'){
//do nothing
}else{
$('.tip').delay(500).fadeIn(1000);
//$.cookie('tip', 'hide', {expires: 7, path: '/'}); // Add this if you want the cookie to disappear on reload (even if they don't click)
}
$('#close').click(function(e) {
$.cookie('tip', 'hide', {expires: 7, path: '/'});
$('.tip').hide();
});
Here is a similar awnser using cookies: jquery change font-size based on cookie?
http://jsbin.com/ufetu5/2/edit
You are passing in cookieOpts
, but the variable is defined below it.
Shift it up to above where it is referenced first.
精彩评论