admin管理员组

文章数量:1287613

I'm using the following to do somting when url contains gender=men&dir=

if(window.location.href.indexOf("gender=men&dir=") > -1) {

      }

Now i need to say url contains gender=men&dir= and not &dir=Schoenen do something

i was trying this only its not working

if(window.location.href.indexOf("gender=men&dir=" || !"&dir=Schoenen") > -1) {

      }

I'm using the following to do somting when url contains gender=men&dir=

if(window.location.href.indexOf("gender=men&dir=") > -1) {

      }

Now i need to say url contains gender=men&dir= and not &dir=Schoenen do something

i was trying this only its not working

if(window.location.href.indexOf("gender=men&dir=" || !"&dir=Schoenen") > -1) {

      }
Share Improve this question asked Jan 23, 2013 at 1:05 IvoIvo 2,6366 gold badges27 silver badges43 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

You have to call indexOf twice:

var hasGender = window.location.href.indexOf("gender=men&dir=") != -1;
var hasDir = window.location.href.indexOf("&dir=Schoenen") != -1;

if ( hasGender && ! hasDir ) {
    // Do whatever you want...
}

|| can't be used that way.

You need to state the window.location.href.indexOf twice:

if (window.location.href.indexOf("gender=men&dir=") > -1) &&
    window.location.href.indexOf("&dir=Schoenen") == -1) {

}

// > -1 is if found
// == -1 is if not found

If you want to change the AND (&&) into an OR (||), you'll need to replace && into ||.

本文标签: Javascript windowlocationhrefindexOf is A and not BStack Overflow