Question

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 regardless of cookies value, any ideas?

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

Solution

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.

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top