Difference between document.addEventListener and window.addEventListener?

后端 未结 3 884
遇见更好的自我
遇见更好的自我 2020-12-02 04:24

While using PhoneGap, it has some default JavaScript code that uses document.addEventListener, but I have my own code which uses window.addEventListener

3条回答
  •  猫巷女王i
    2020-12-02 05:22

    The window binding refers to a built-in object provided by the browser. It represents the browser window that contains the document. Calling its addEventListener method registers the second argument (callback function) to be called whenever the event described by its first argument occurs.

    Some paragraph.

    Following points should be noted before select window or document to addEventListners

    1. Most of the events are same for window or document but some events like resize, and other events related to loading, unloading, and opening/closing should all be set on the window.
    2. Since window has the document it is good practice to use document to handle (if it can handle) since event will hit document first.
    3. Internet Explorer doesn't respond to many events registered on the window,so you will need to use document for registering event.

提交回复
热议问题