简体   繁体   中英

Checking cookie name using jquery wildcard

I have a form generating dynamic cookie, say:

webform-62[1234356]

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.

Thanks.

Use this:

if (document.cookie.search(/\bwebform-\S*=/) < 0) {
    // no cookie
}

document.cookie contains a list of cookies on the current domain/path in the following format:

<cookie-name>=<cookie-value> (each name=value pair is separated by a ; ).

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.

你需要遍历所有的cookie并匹配你想要的东西,这里有一些如何做的例子: http//www.electrictoolbox.com/javascript-get-all-cookies/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM