Get the current URL via JavaScript

There's actually two different ways to get the current URL. Both document.URL and document.location.href return the URL. However, if you wish to actually change the document's location, only document.location.href. If you only need to read the value, then document.URL is fine.

Note that if you wish to work with the various parts of the current location, inspect document.location. It has properties for things like the port, protocol, etc.

var ourLocation = document.URL;
console.log("Currently at " + ourLocation);
Published: 8/15/2013
More Information: https://developer.mozilla.org/en-US/docs/Web/API/document.URL
Tags: misc
comments powered by Disqus