admin管理员组文章数量:1391974
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 }
本文标签: javascriptJQuery Cookies CreationCheck if cookie already has a valueStack Overflow
版权声明:本文标题:javascript - JQuery Cookies Creation...Check if cookie already has a value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732637a2622140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论