当(且仅当)它不存在时创建一个 cookie [英] Create a cookie if (and only if) it doesn't already exist
本文介绍了当(且仅当)它不存在时创建一个 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想:
- 检查是否存在名称为query"的 cookie
- 如果是,那么什么都不做
- 如果不是,则创建一个值为 1 的 cookie查询"
注意:我使用的是 jQuery 1.4.2 和 jQuery cookie 插件.
Note: I am using jQuery 1.4.2 and the jQuery cookie plugin.
有人对我如何做到这一点有任何建议吗?
Does anyone have any suggestions as to how I can do this?
推荐答案
if($.cookie('query') === null) {
$.cookie('query', '1', {expires:7, path:'/'});
}
或者,您可以为此编写一个包装函数:
Alternatively, you could write a wrapper function for this:
jQuery.lazyCookie = function() {
if(jQuery.cookie(arguments[0]) !== null) return;
jQuery.cookie.apply(this, arguments);
};
然后你只需要在你的客户端代码中写下这个:
Then you'd only need to write this in your client code:
$.lazyCookie('query', '1', {expires:7, path:'/'});
这篇关于当(且仅当)它不存在时创建一个 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文