63

What is the difference between document.location.href and document.location?

Is it the same across browsers?

3
  • document.location is an object
    – ant
    Commented Apr 16, 2010 at 12:21
  • 1
    Everything is an object. I think what you mean is that unlike location.href, it isn't a simple string. Commented Apr 16, 2010 at 12:28
  • 1
    @Max Shawabkeh yes location.href is a string
    – ant
    Commented Apr 16, 2010 at 12:36

7 Answers 7

73

document.location is a synonym for window.location that has been deprecated for almost as long as JavaScript has existed. Don't use it.

location is a structured object, with properties corresponding to the parts of the URL. location.href is the whole URL in a single string. Assigning a string to either is defined to cause the same kind of navigation, so take your pick.

I consider writing to location.href = something to be marginally better as it's slightly more explicit about what it's doing. You generally want to avoid just location = something as it looks misleadingly like a variable assignment. window.location = something is fine though.

6
  • 7
    Does it document.location or window.location which is deprecated?
    – enguerran
    Commented Apr 10, 2013 at 15:22
  • 9
    document.location. It's no longer deprecated in HTML5 now, for what it's worth.
    – bobince
    Commented Apr 10, 2013 at 16:21
  • 1
    They are defined to all work the same, yes. location is the same reference as window.location as long as you haven't defined a local variable with that name; it's not clear whether the spec requires that document.location===window.location, but it does in current browsers.
    – bobince
    Commented Apr 11, 2013 at 12:39
  • 1
    who else is here because typescript 2.0 made document.location a readonly property and your code doesn't compile anymore ;-) [the answer of course is document.location.href instead] Commented Nov 3, 2016 at 4:21
  • 7
    This thread is ancient but I want to point out that they mean different things, in part because the window object can contain more than one document. In a web page rendered in a browser, the value of window.location will typically match the (potentially read-only) document,location.href property, but it's the former -- window.location -- that should be considered mutable and suitable for triggering navigation.
    – cweekly
    Commented Jan 15, 2019 at 20:22
32

The document.location is an object that contains properties for the current location.

The href property is one of these properties, containing the complete URL, i.e. all the other properties put together.

Some browsers allow you to assign an URL to the location object and acts as if you assigned it to the href property. Some other browsers are more picky, and requires you to use the href property. Thus, to make the code work in all browsers, you have to use the href property.

Both the window and document objects has a location object. You can set the URL using either window.location.href or document.location.href. However, logically the document.location object should be read-only (as you can't change the URL of a document; changing the URL loads a new document), so to be on the safe side you should rather use window.location.href when you want to set the URL.

0
13
typeof document.location; // 'object'
typeof document.location.href; // 'string'

The href property is a string, while document.location itself is an object.

1
  • The dot-notation alone clarifies this
    – arkon
    Commented Oct 2, 2012 at 14:51
6

document.location is an object, while document.location.href is a string. But the former has a toString method, so you can read from it as if it was a string and get the same value as document.location.href.

In some browsers - most modern ones, I think - you can also assign to document.location as if it were a string. According to the Mozilla documentation however, it is better to use window.location for this purpose as document.location was originally read-only and so may not be as widely supported.

3

document.location is deprecated in favor of window.location, which can be accessed by just location, since it's a global object.

The location object has multiple properties and methods. If you try to use it as a string then it acts like location.href.

3

As of June 14th 2013 (HTML5), there is a significant difference

Browser : Chrome 27.X.X

References: document.location, window.location (MDN)


document.location

type: Object

The object when called by itself document.location return its origin + pathname properties concatenated.

To retrieve just the URL as a string, the read-only document.URL property can be used.

ancestorOrigins: DOMStringList
assign: function () { [native code] }
hash: ""
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "http://stackoverflow.com/questions/2652816/what-is-the-difference-between-document-location-href-and-document-location?rq=1"
origin: "http://stackoverflow.com"
pathname: "/questions/2652816/what-is-the-difference-between-document-location-href-and-document-location"
port: ""
protocol: "http:"
reload: function () { [native code] }
replace: function () { [native code] }
search: "?rq=1"
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }

document.location.href

type: string

http://stackoverflow.com/questions/2652816/what-is-the-difference-between-document-location-href-and-document-location?rq=1
0
0

Here is an example of the practical significance of the difference and how it can bite you if you don't realize it (document.location being an object and document.location.href being a string):

We use MonoX Social CMS (http://mono-software.com) free version at http://social.ClipFlair.net and we wanted to add the language bar WebPart at some pages to localize them, but at some others (e.g. at discussions) we didn't want to use localization. So we made two master pages to use at all our .aspx (ASP.net) pages, in the first one we had the language bar WebPart and the other one had the following script to remove the /lng/el-GR etc. from the URLs and show the default (English in our case) language instead for those pages

<script>
  var curAddr = document.location; //MISTAKE
  var newAddr = curAddr.replace(new RegExp("/lng/[a-z]{2}-[A-Z]{2}", "gi"), "");
  if (curAddr != newAddr)
    document.location = newAddr;
</script>

But this code isn't working, replace function just returns Undefined (no exception thrown) so it tries to navigate to say x/lng/el-GR/undefined instead of going to url x. Checking it out with Mozilla Firefox's debugger (F12 key) and moving the cursor over the curAddr variable it was showing lots of info instead of some simple string value for the URL. Selecting Watch from that popup you could see in the watch pane it was writing "Location -> ..." instead of "..." for the url. That made me realize it was an object

One would have expected replace to throw an exception or something, but now that I think of it the problem was that it was trying to call some non-existent "replace" method on the URL object which seems to just give back "undefined" in Javascript.

The correct code in that case is:

<script>
  var curAddr = document.location.href; //CORRECT
  var newAddr = curAddr.replace(new RegExp("/lng/[a-z]{2}-[A-Z]{2}", "gi"), "");
  if (curAddr != newAddr)
    document.location = newAddr;
</script>

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