tags:

views:

223

answers:

6

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 ?

+6  A: 

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.

Artem Barger
I just use location!
Josh Stodola
+2  A: 
window.location.href;

or

location.href;

EDIT: Christoph is correct.

Joel Potter
`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)
Christoph
A: 

document.URL

Daniel Moura
+2  A: 

What you are looking for is window.location.href

byte
+2  A: 

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...

Adam Lerman
A: 

Use window.location for read and write access to the location object associated with the current frame. If you just want to get the address as a read-only string, you may use document.URL, which should contain the same value as window.location.href.

Christoph
see also http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascript/2431375#2431375
Christoph