image.addEventListener("load", function() {} ); will not work in all browsers, but provided that you have a backup and use image.attachEvent("onload", function() {})
if(image.addEventListener) { image.addEventListener("load",function() {}); } else { image.attachEvent("onload", function() {}); }
This will have the advantage that you do not override any onload event that already exists in the image, and that your onload event will not be overridden by any other code.
Directly changing the "onload" attribute of a DOM element is usually not recommended just because you might think "Hey, I can save a couple lines of code by setting it, and I only need one listener, so I just set it, and not use addEventListener / attachEvent "but it invariably leads to" Well, if I need to add something even later? "
For this reason, javascript developers usually use a library to add events, so you can confidently add a listener with a single line of code, and it will work in all browsers.
source share