Reputation: 7688
Ok so I am using this, https://github.com/carhartl/jquery-cookie, jquery plugin and I need to do an if (null){} else{}
but I can't get it to work
$('.ff').click(function(){
if ($.cookie('ff') == null)
{
$.cookie('ff', 'on', { expires: 1, path: '/' });
alert($.cookie('ff')+' on?');
}
if ($.cookie('ff') == 'on')
{
$.cookie('ff', null);
alert($.cookie('ff')+' offFFFFFFFFFFFFFFff?');
}
});
What am I doing wrong?
Upvotes: 0
Views: 2408
Reputation: 126052
Change your second if
to an else if
. Also, according to the plugin documentation:
Note: when deleting a cookie, you must pass the exact same path, domain and secure options that were used to set the cookie.
So make sure to pass the same path
option when you delete the cookie:
if ($.cookie('ff') == null)
{
$.cookie('ff', 'on', { expires: 1, path: '/' });
alert($.cookie('ff')+' on?');
}
else if ($.cookie('ff') == 'on')
{
$.cookie('ff', null, { path: '/' });
alert($.cookie('ff')+' offFFFFFFFFFFFFFFff?');
}
If $.cookie('ff')
was null previously, it was first being set to 'on'
inside the first if
block. Then the second condition was being evaluated to true, causing the cookie to be set to null
again.
Example: http://jsfiddle.net/andrewwhitaker/azHK2/
Upvotes: 1