2

I want to do he following, but it does not work:

if(pathname == '/ik/services/' || '/ik/recruitment/'){
   //run function
}

It is completely ignoring my if statement and executes the code for all pages...

nbrooks
  • 18,126
  • 5
  • 54
  • 66
David Van Staden
  • 1,739
  • 9
  • 34
  • 52

5 Answers5

8

You would have to do something like this:

if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){
   //run function
}

Your || '/ik/recruitment/' would always be truthy, and therfor the code within your if-statement will always run.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • 5
    Upvote for being the only answer out of five that actually explains why the OP's code doesn't work. – JJJ Apr 09 '13 at 12:01
  • @rrikesh Weird, I swear it worked just a minute ago. I'll try to track down another good resource on truthy/falsy and update my answer. Thanks for the heads up! – Christofer Eliasson Apr 09 '13 at 12:07
  • 1
    The Google Cache says it was available in March. But I find it strange that is not found right now – RRikesh Apr 09 '13 at 12:09
  • @DavidVanStaden My pleasure! Updated my answer with another read on truthy/falsy values, that hopefully works better. – Christofer Eliasson Apr 09 '13 at 12:15
4

Try

if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){

OR

jQuery inArray

Example

var arr = ['/ik/services/','/ik/recruitment/'];

if($.inArray(pathname , arr) !== -1)
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
2

This has nothing to do with jQuery, it's just a normal JS "error". You need to compare both strings with the variable:

if (pathname == 'foo'  || pathname == 'bar') 
powerbuoy
  • 12,460
  • 7
  • 48
  • 78
1

Try this:

if((pathname == '/ik/services/') || (pathname == '/ik/recruitment/')){
   //run function
}
palaѕн
  • 72,112
  • 17
  • 116
  • 136
1

try doing

    if(pathname == '/ik/services/' || pathname == '/ik/recruitment/'){
      //run function
    }
Harish
  • 1,469
  • 16
  • 43