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 16:49

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.

查看更多
浮光初槿花落
3楼-- · 2018-12-31 16:52

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.

查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 16:56

window.location is the more reliably consistent of the two, considering older browsers.

查看更多
浅入江南
5楼-- · 2018-12-31 16:57

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!')
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 16:58

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

查看更多
梦寄多情
7楼-- · 2018-12-31 16:58

Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:

window.location === document.location
查看更多
登录 后发表回答