Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I get the website URL?

Not the URL as taken from a link. On the loading page, I want to get the full, current URL of the current web page and store its value in a variable.

share|improve this question

3 Answers

up vote 671 down vote accepted

Use:

alert(document.URL);

See URL of type DOMString, readonly.

share|improve this answer
41  
In Firefox 12 the document.URL property doesn't update after a window.location to an anchor (#), while window.location.href does. I've put together a live demo here: jsfiddle.net/PxgKy I didn't test any other versions of Firefox. No issues using document.URL were found in Chrome 20 and IE9. –  Telmo Marques Jul 7 '12 at 16:26
12  
also you can get host and clear location: window.location.host and window.location.href.toString().split(window.location.host)[1] –  ali youhannaei Nov 26 '12 at 11:59
 
@Telmo — This looks like a bug in Firefox. –  Nicolas Barbulesco May 23 at 17:29
 
and what's document.baseURI about then. Basically there are 3 ways to get url document.baseURI, document.URL, & location. –  Muhammad Umer Aug 29 at 12:06

The same question has been asked less than 24 hours ago. To quote myself:

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.

share|improve this answer
6  
see also stackoverflow.com/questions/2430936/… –  Christoph Mar 12 '10 at 9:06

Gets the current page URL:

window.location.href
share|improve this answer
2  
Note that that’s the window’s location not the document’s. –  Gumbo Jun 23 '09 at 19:32
11  
It's the same thing. Full current URL refers to the document path (external address). –  Zanoni Jun 23 '09 at 19:34
2  
Is it standardized like document.url? (I mean something like a w3c document) –  chendral Jun 23 '09 at 19:47
5  
@chendral: w3.org/TR/Window/#location –  Christoph Jun 23 '09 at 21:42

protected by minitech May 30 at 2:56

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.