tags:

views:

30

answers:

3

Basically I want to check what url the user is on and compare it to a default value and then run a statement

Let's say my url is http://stackoverflow.com/questions/ask, I'm trying to do this, and it's not working:

<script type="text/javascript">
      if(document.location.href('/questions/ask') > 0)
  {
    [do this];
  }

Thanks for the help(noob question I know)

+1  A: 

Give this a try, you are missing the indexOf method.

if(document.location.href.indexOf('/questions/ask') > -1)

But I believe you should be going off of the window object, I think document is deprecated (but still works).

if(window.location.href.indexOf('/questions/ask') > -1)

You also want to check to see if the index is greater than negative one because zero is technically a correct position.

Bob
yeah that works thanks
Alex
No problem, glad to help.
Bob
A: 

Try this:

var loc = new String(window.location);
if(loc.match('/questions/ask')) {
    // do something here
}
Andrei Serdeliuc
A: 

window.location.href gives you the whole url. You can also access other properties of location, like protocol, pathname, hash, host.

if(window.location.pathname == '/questions/ask')
seanmonstar