Filippo oretti
Filippo oretti

Reputation: 49843

JS check if url contains word or chars

is there anyway to check if window.location.href contains some portions chars or words?

like:

if(window.location.href.like("/users") or window.location.href.like("/profile") ){
//do somenthing
}

Upvotes: 0

Views: 1612

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

window.location.href.search("something") is useful

if(window.location.href.search(/\/(users|profile)/) != -1) {  // if not found
  // do something
}

search() return index of first match or -1 if not found, doesn't return boolean true/false

You can also use

if(!(/\/("users|profile")/.test(window.location.href))) {  // if not found
  // do something
}

.test() return boolean value.

Upvotes: 1

user229044
user229044

Reputation: 239382

You can use .match and supply a regular expression:

if (window.location.href.match(/\/(users|profile)/)) {
  alert('yes');
}

Upvotes: 1

Related Questions