开发者

jQuery if Cookie Value does not exist of multiple IndexOf's

I've got a cookie I am creating with jQuery named 'testCookie'. I want to check to see if any of these VALUES DO NOT exist, if they do NOT (or are equal to or less than -1) I want to do something, the code below currently does nothing and it assumes as if the code isn't even there and loads everything after the if-statement regardles开发者_开发技巧s of cookies value, any ideas?

if ($.cookie('testCookie').indexOf('shopping','pricegrabber','nextag','shopzilla')<=-1) {


The indexOf function can only take one string at a time. To do this you would need to have multiple clauses in your if statement, joined with &&:

if(cookieValue.indexOf('shopping') == -1 && cookieValue.indexOf('pricegrabber') == -1)

You can add all of your conditions into that if statement. The && means "if this and this" etc. etc.


the syntax of indexOf() is like this:

['a', 'b', 'c', 'd'].indexOf('b'); // returns 1

you can use jQuery's .inArray() method:

$.inArray('b', ['a','b','c','d']); // returns 1

if $.cookie('testCookie') returns string you can check it like this:

if (['grabber','nextag','shopzilla'].indexOf($.cookie('testCookie')) == -1)  {
    // your code
}

or

if ($.inArray($.cookie('testCookie', ['grabber','nextag','shopzilla']) == -1) {
    // your code
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜