What is the difference between addEventListener and attachEvent?

This is the code I used on my page,

if (window.addEventListener) { window.addEventListener("load", createIframe, false); } else if (window.attachEvent) { window.attachEvent("onload", createIframe); } else { window.onload = createIframe; } 

Please explain to me where my createIframe funtion is called ? and what is the difference between addEventListener and attachEvent? and what is the difference between load and load? completely confused to find the difference between addEventLisener with download and attachEvent with onload

+6
source share
1 answer

Quick answer: you should use attachEvent if your browser returns undefined == window.addEventListener . Thing is a non-standard JS function implemented in IE8 and previous versions, and addEventListener supported by IE9 + (and all other browsers).

So the big question is: are you going to support IE8 -?

Margin note: window.onload = whatever will override any connected event listeners. This is why addEventListener used: to add a function to the event stack instead of writing it.

+12
source

Source: https://habr.com/ru/post/989345/


All Articles