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
  • 17,869
  • 5
  • 51
  • 65
David Van Staden
  • 1,669
  • 8
  • 34
  • 51

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
  • 31,342
  • 6
  • 71
  • 100
  • 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
  • 26,601
  • 7
  • 57
  • 86
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
  • 11,711
  • 5
  • 41
  • 73
1

Try this:

if((pathname == '/ik/services/') || (pathname == '/ik/recruitment/')){
   //run function
}
palaѕн
  • 68,816
  • 17
  • 108
  • 129
1

try doing

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