Raja
Raja

Reputation: 305

If statement on URL wildcard

How can I accomplish an if statement that says in plain english. If this url that has "W" in it do not show.

I have this.. I just need the wilcard type expression.

if (window.location.href == 'http://support.com/support/default.asp?W2297') {
 $(".navi").hide();
}

how can i hide some elements if url has W**** at the end.. i want it to use every url http://support.com/support/default.asp?W* that finishes with a W

Upvotes: 0

Views: 204

Answers (2)

Amit
Amit

Reputation: 46341

You can do that with a simple regex. Here's how it works:

var arr = ['http://support.com/support/default.asp?W2297',
           'http://support.com/support/default.asp?W22',
           'http://support.com/support/default.asp?X2297']

for(var i = 0; i < arr.length; i++)
  alert(arr[i] + ': ' + /\?W.+$/.test(arr[i]))

Note: This regex looks for precisely a question mark ('?') followed by an uppercase 'W' followed by 1 or more characters. If that's not precisely what you need, you'll have to either clarify or correct the regex yourself...

Your code can be:

if (/\?W.+$/.test(window.location.href)) {
  $(".navi").hide();
}

Upvotes: 6

You can say

if ( location.href.lastIndexOf( "W" ) > location.href.lastIndexOf( "/" ) ) {
 $(".navi").hide();
}

This will hide the .navi everytime there is a "W" in the request ( ?*** ) part.

More about lastIndexOf, if You are interested.

Upvotes: 0

Related Questions