58

Possible Duplicate:
Get current URL with JavaScript?

How do you get the address of the page you are on in JavaScript?

For example, if I had a script at somesite.com/javascript/home.html and I want to find out the request address (somesite.com/javascript/home.html), how do I get this information in JavaScript?

2
  • 14
    How was this one marked as a dupe if it was asked first? Commented Apr 29, 2014 at 16:07
  • 2
    Life just isn't fair - the duplicate duped this original into a lonely corner, stole all the reputation and then said that this one was the duplicate. Commented Nov 13, 2014 at 10:09

6 Answers 6

38

You need to use: document.location or window.location

You can read more here. Or there is a little more explanation over there.

For clarifying matter:

Originally Posted by Mozilla Developer Center

document.location was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, use window.location instead.

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

Comments

13
window.location.href;

or

location.href; 

window is the global object, so location.href will be identical to window.location.href and NOT document.location.href (as long as there's no enclosing function or with statement which shadows the property)

Comments

8

What you are looking for is window.location.href.

Comments

4

I believe that either the window.location.href or the window.location.pathname objects will have this information.Someone could confirm or deny that though.

1 Comment

Nope. window.location.href will return the full url including the query strings e.g domain.com/test.php?param=1&req=1 while window.location.pathname will return just the path e.g test.php :)
2

document.URL

Comments

2
document.location.href

or

window.location.href

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.