关于一个location对象的问题

window.location 和 window.document.location 有什么本质性的区别吗?

阅读 3.7k
5 个回答

没有区别, 都是location对象的引用
window.location == document.location

javascriptwindow.location === document.location;    // true

根据 W3C 规范:

The location attribute of the Window interface must return the Location object for that Window object's Document.

window 的 location 属性指的就是 window 对象的 document 属性的 location,二者是一回事。所以:

window.location === window.document.location   // true

The Location interface

Each Document object in a browsing context's session history is associated with a unique instance of a Location object.

document . location [ = value ]
window . location [ = value ]
Returns a Location object with the current page's location.

Can be set, to navigate to another page.
明显,两者是相等的

推荐问题