I am using this code to create a cookie with JQuery:
$.cookie('MyCookieName', 'myValueHere');
It works fine but as I have the value assign to a random number it's generating a new one every reload.
What I need to do is check if cookie has a value and if it's not empty then don't create a new one or generate any new value.
I am using this code to create a cookie with JQuery:
$.cookie('MyCookieName', 'myValueHere');
It works fine but as I have the value assign to a random number it's generating a new one every reload.
What I need to do is check if cookie has a value and if it's not empty then don't create a new one or generate any new value.
Share Improve this question edited Feb 3, 2012 at 11:43 Manse 38.1k11 gold badges86 silver badges111 bronze badges asked Feb 3, 2012 at 11:40 Satch3000Satch3000 49.5k90 gold badges225 silver badges349 bronze badges3 Answers
Reset to default 2if ($.cookie('MyCookieName') == null) {
$.cookie('MyCookieName','MyCookieValue');
}
You can also add expire time and path in options.
$.cookie('MyCookieName','MyCookieValue', { path: '[path here]', expires: [days] });
Then you can be sure, that cookies won't be deleted.
Try checking for a value already set in an if statement like this
if($.cookie('MyCookieName') == null) {
//it doesnt exist
} else {
// do something else maybe
}
Thats if your plugin for example this one returns null
when the cookie is not found
to avoid misunderstandings it's better to use exact parison:
if($.cookie('MyCookieName') === null) { //some stuff }