1

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 https://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)

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

Comments

0

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.