What's the difference between window.location and document.location? Should both of them reference the same object?
According to the W3C, they are the same. In reality, for cross browser safety, you should use window.location
rather than document.location
.
window.location
) without providing any justification for it. If you won't provide the justification, why should anyone take your advice? Christoph's answer is far more useful in this regard. –
Tailrace The canonical way to get the current location object is window.location
(see this MSDN page from 1996 and the W3C draft from 2006).
Compare this to document.location
, which originally only returned the current URL as a string (see this page on MSDN). Probably to avoid confusion, document.location
was replaced with document.URL
(see here on MSDN), which is also part of DOM Level 1.
As far as I know, all modern browsers map document.location
to window.location
, but I still prefer window.location
as that's what I've used since I wrote my first DHTML.
window.location
, isnt it equally valid to just use location
? –
Proselyte window
object. Thus, any variable or function you define at the top level of your script, is a property of the object referenced by window
, which happens to be the global object. Global object is implied when absent like window.
-- thus location
is interpreted to be window.location
. Caveats -- f.e. if(an_undefined_variable)
will throw an error if variable wasn't defined -- if(window.an_undefined_variable)
won't. –
Pentamerous location
in a local scope. –
Xmas window.location is read/write on all compliant browsers.
document.location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).
document.location
is read-only in IE. I can successfully assign to it in IE 10, 9, 8 and 6 (using VMs from modern.ie). –
Tailrace document.location
was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, use window.location
instead.
Read more:
Interestingly, if you have a frame, image, or form named 'location', then 'document.location' provides a reference to the frame window, image, or form, respectively, instead of the Location object. Apparently, this is because the document.forms, document.images, and window.frames collection name lookup gets priority over the mapping to window.location.
<img name='location' src='location.png'>
if (document.location.tagName == 'IMG') alert('Hello!')
window.location
and document.location
cannot be shadowed in Chrome or Firefox. –
Septime As far as I know, Both are same. For cross browser safety you can use window.location
rather than document.location
.
All modern browsers map document.location
to window.location
, but I still prefer window.location
as that's what I've used since I wrote my first web page. it is more consistent.
you can also see document.location === window.location
returns true
, which clarifies that both are same.
document.location === window.location
returns true
also
document.location.constructor === window.location.constructor
is true
Note: Just tested on , Firefox 3.6, Opera 10 and IE6
===
and ==
are equivalent. –
Tailrace "abc" == new String("abc")
returns true
while "abc" === new String("abc")
returns false
. –
Sapphira ==
and ===
are equivalent. See the spec sections 11.9.3 and 11.9.6. For non-null, non-undefined, non-number, non-bool, non-string values with the same type, ==
behavior is governed by 11.9.3 part 1f, and ===
behavior by 11.9.6 part 7, which identically read Return true
if x and y refer to the same object. Otherwise, return false
. –
Tailrace document.location
and window.location
are pointing to objects. You are missing the whole point of triple equals; using 2 equals does not prove that they are the same obj. We should use 3 equals and not 2 equals because 2 equals will give us a false positive. On a browser whereby document.location is a URL string equal to window.location.toString()
, Then document.location==window.location
will return true while document.location===window.location
will return false. –
Sapphira document.location === window.location
comparison goes. The fact that the .constructor
comparison is thrown in too means, I think, that this answer is still sound, but using ===
would simplify the reasoning. –
Tailrace .constructor
property of a String
or Number
or Boolean
instance. In short, YOU's code may work, but it's definitely not cross browser, and therefore it doesn't prove what it's supposed to prove. –
Sapphira Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:
window.location === document.location
window.location is the more reliably consistent of the two, considering older browsers.
It's rare to see the difference nowadays because html 5 don't support framesets anymore. But back at the time we have frameset, document.location would redirect only the frame in which code is being executed, and window.location would redirect the entire page.
document.location.constructor === window.location.constructor
is true
.
It's because it's exactly the same object as you can see from document.location===window.location
.
So there's no need to compare the constructor or any other property.
At least in IE, it has a little difference on local file:
document.URL will return "file://C:\projects\abc\a.html"
but window.location.href will return "file:///C:/projects/abc/a.html"
One is back slash, one is forward slash.
Well yea, they are the same, but....!
window.location
is not working on some Internet Explorer browsers.
I would say window.location
is the more reliable way of getting the current URL.
Following is the difference between the window.location
and document.url
that came in front in one of the scenarios where I was appending hash parameters in the URL and reading it later.
After adding hash parameters in the URL.
In an older browser, I was not able to get the hash parameters from the URL by using document.url
, but when I used window.location
then I was able to get the hash parameters from the URL.
So it's always better to use window.location
.
document.URL
- it was about window.location
and document.location
. Also, document.url
doesn't exist = it should be uppercase. –
Tailrace I prefer using document.location
, even though location
, document.location
, and window.location
returns same object.
Reasons for using document.location
are:
- Browser compatibility section of window.location mentions
Before Firefox 57, single quotes contained in URLs were escaped when accessed via URL APIs. See bug 1386683.
- Browser compatibility section of document.location mentions
Full support.
Mdn location reference uses
document.location
in their examples.// location: https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container const loc = document.location; console.log(loc.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container console.log(loc.protocol); // https: console.log(loc.host); // developer.mozilla.org:8080 console.log(loc.hostname); // developer.mozilla.org console.log(loc.port); // 8080 console.log(loc.pathname); // /en-US/search console.log(loc.search); // ?q=URL console.log(loc.hash); // #search-results-close-container console.log(loc.origin); // https://developer.mozilla.org:8080 location.assign('http://another.site') // load another page
tl;dr They are virtually always the same. Prefer window.location
.
The Document object's location getter steps are to return this's relevant global object's Location object, if this is fully active, and null otherwise.
The Window object's location getter steps are to return this's Location object.
https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location
When are they different? If the document is not fully active. For example:
const iframe = document.createElement("iframe");
document.body.append(iframe);
const { contentDocument, contentWindow } = iframe;
iframe.remove();
contentWindow.location; // not null
contentDocument.location // null
Despite of most people recommend here, that is how Google Analytics's dynamic protocol snipped looked like for ages (before they moved from ga.js to analytics.js recently):
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
More info: https://developers.google.com/analytics/devguides/collection/gajs/
In new version they used '//' so browser can automatically add protocol:
'//www.google-analytics.com/analytics.js'
So if Google prefers document.location to window.location
when they need protocol in JS, I guess they have some reasons for that.
OVERALL: I personally believe that document.location
and window.location
are the same, but if giant with biggest stats about usage of browsers like Google using document.location, I recommend to follow them.
Actually I notice a difference in chrome between both , For example if you want to do a navigation to a sandboxed frame from a child frame then you can do this just with document.location but not with window.location
© 2022 - 2024 — McMap. All rights reserved.