Difference between document.URL and location.href

JavascriptUrlDomLocation

Javascript Problem Overview


I know that document.URL can not be set, while location.href can.

But the Document indicates:

> URL is a replacement for the DOM Level 0 location.href property.

So when would we use document.URL?

Javascript Solutions


Solution 1 - Javascript

You can get the document.URL, but you can not set it. You can both get and set the location.href.

In some webbrowsers, you are able to set the document.URL but please don't, as it doesn't work in most browsers.

You gave the answer yourself!

var currentURL = document.URL;
alert(currentURL);

Learn more here

Solution 2 - Javascript

They're interchangeable as far as getting data is concerned, but as you pointed out document.URL can not be set. I just always use location.href since it's a getter/setter.

Solution 3 - Javascript

Yes and no!

alert(document.url);  
document.url="http://www.google.co.uk";  
alert(document.url);  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioneddie yangView Question on Stackoverflow
Solution 1 - Javascriptuser657496View Answer on Stackoverflow
Solution 2 - JavascriptNoneView Answer on Stackoverflow
Solution 3 - Javascriptpop stackView Answer on Stackoverflow