开发者

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');
}

A开发者_如何学编程ny 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.


you need to loop through all cookies and match what you are looking for, here is some example of how to do it: http://www.electrictoolbox.com/javascript-get-all-cookies/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜