64

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?

2
  • 2
    Whenever you want to read the URL... Commented Mar 22, 2011 at 7:33
  • 1
    ....in a "modern" manner. document.location.href is for ancients.
    – Pacerier
    Commented Oct 11, 2014 at 17:25

3 Answers 3

67

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

1
  • 15
    The real question: why does document.URL exist when you can do everything with location.href? Commented May 27, 2019 at 5:31
6

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.

-10

Yes and no!

alert(document.url);  
document.url="http://www.google.co.uk";  
alert(document.url);  
1
  • 11
    We are talking about URL and not url. Case matters in Javascript.
    – Pacerier
    Commented Oct 11, 2014 at 17:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.