What's the difference between [removed] and [removed] in JavaScript?

后端 未结 16 1972
故里飘歌
故里飘歌 2020-11-22 12:37

Should both of them reference the same object?

相关标签:
16条回答
  • 2020-11-22 13:37

    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

    0 讨论(0)
  • 2020-11-22 13:38

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

    0 讨论(0)
  • 2020-11-22 13:39

    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!')
    
    0 讨论(0)
  • 2020-11-22 13:39

    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.

    0 讨论(0)
提交回复
热议问题