What is the difference between using 'window.addEventListener' and 'window.document.addEventListener'

4

What is the difference between using window.addEventListener and window.document.addEventListener

window.addEventListener("keydown", function(k){

if(k.altKey) {
alert("Solamente window.addEventListener: Tecla [ALT] PRESIONADA");
}
else {
alert("Solamente window.addEventListener: Tecla [ALT], NO PRESIONADA");
}

}, false);


window.document.addEventListener("keydown", function(k){

if(k.altKey) {
alert("Con window.document.addEventListener: Tecla [ALT] PRESIONADA");
}
else {
alert("Con window.document.addEventListener: Tecla [ALT], NO PRESIONADA");
}

}, false);
<html>
<body>
</body>
</html>
    
asked by Eduardo Sebastian 26.06.2017 в 22:19
source

1 answer

5

For this case there is no difference. But maybe these links are more useful: link and link

They explain the difference of window and document :

Document

It is the root component of the application, and the other nodes including window are hijos of this. So when you assign an event to the document you will always capture it.

Window

It is a component that represents the window, in the example it would also capture the event because it is the only window that exists in the application.

But it can behave differently if you have several windows, which can be achieved with the iframe tag, which creates a window object for each iframe you have, in this case it would only capture the window event that you have selected.

For more information link .

Also clarify that window is a global variable, which has as a document member, which is the root node

    
answered by 26.06.2017 / 22:34
source