使用jquery通配符检查cookie名称 [英] Checking cookie name using jquery wildcard
问题描述
我有一个表单生成动态Cookie,例如:
I have a form generating dynamic cookie, say:
webform-62[1234356]
62 [1234356] 可以是任何数字。我需要检查是否存在以webform-开头的cookie使用一些通配符检查。以下不工作
62[1234356] can be any number. I need to check if cookie with name starting with "webform-" exists using some wildcard check. Below is not working
if( $.cookie('webform\\S*') == null ) {
alert('no cookie');
}
任何提示都将非常感谢。
Any hint would be very much appreciated.
感谢。
推荐答案
使用:
if (document.cookie.search(/\bwebform-\S*=/) < 0) {
// no cookie
}
document.cookie
包含以下格式的当前域/路径上的Cookie列表:
document.cookie
contains a list of cookies on the current domain/path in the following format:
< cookie-name> =& value>
(每个名称=值对由;
分隔)。
<cookie-name>=<cookie-value>
(each name=value pair is separated by a ;
).
因此,给定的代码只是在此列表中搜索名为 webform -\S *
的cookie。如果找到cookie,搜索调用返回大于或等于0的数字。
So the given code just searches for a cookie with a name of the form webform-\S*
in this list. The search call returns a number upper than or equal to 0 if the cookie is found.
这篇关于使用jquery通配符检查cookie名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!