What's the difference between [removed] and [r

2018-12-31 16:26发布

Should both of them reference the same object?

16条回答
浮光初槿花落
2楼-- · 2018-12-31 17:00

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).

查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 17:00

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:

document.location

window.location

查看更多
唯独是你
4楼-- · 2018-12-31 17:01

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.

查看更多
泛滥B
5楼-- · 2018-12-31 17:02

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.

查看更多
冷夜・残月
6楼-- · 2018-12-31 17:09

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.

查看更多
余生请多指教
7楼-- · 2018-12-31 17:11

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

查看更多
登录 后发表回答