57

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?

Community
  • 1
  • 1
LB.
  • 13,730
  • 24
  • 67
  • 102
  • 14
    How was this one marked as a dupe if it was asked first? – Mkalafut Apr 29 '14 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. – icc97 Nov 13 '14 at 10:09

6 Answers6

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.

aug
  • 11,138
  • 9
  • 72
  • 93
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
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)

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Joel
  • 19,175
  • 2
  • 63
  • 83
8

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

fedorqui
  • 275,237
  • 103
  • 548
  • 598
dreadwail
  • 15,098
  • 21
  • 65
  • 96
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.

Hiranya Sarma
  • 1,454
  • 4
  • 15
  • 25
Adam Lerman
  • 3,369
  • 9
  • 42
  • 53
  • Nope. window.location.href will return the full url including the query strings e.g http://domain.com/test.php?param=1&req=1 while window.location.pathname will return just the path e.g test.php :) – Woppi Oct 31 '12 at 02:10
2
document.location.href

or

window.location.href
Talha
  • 18,898
  • 8
  • 49
  • 66
2

document.URL

Daniel Moura
  • 7,816
  • 5
  • 35
  • 60