Should both of them reference the same object?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I would say
window.location
is the more reliable way of getting the current URL. Following is the difference between thewindow.location
anddocument.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 usedwindow.location
then I was able to get the hash parameters from the URL.So it's always better to use
window.location
.Well yea, they are the same, but....!
window.location
is not working on some Internet Explorer browsers.According to the W3C, they are the same. In reality, for cross browser safety, you should use
window.location
rather thandocument.location
.See: http://www.w3.org/TR/html/browsers.html#dom-location
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):
More info: https://developers.google.com/analytics/devguides/collection/gajs/
In new version they used '//' so browser can automatically add protocol:
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
andwindow.location
are the same, but if giant with biggest stats about usage of browsers like Google using document.location, I recommend to follow them.