For detecting when an image has completed downloading which method should I use?
image.onload = function () {}
or
image.addEventListener("load", function () {} );
For detecting when an image has completed downloading which method should I use?
image.onload = function () {}
or
image.addEventListener("load", function () {} );
onload:
onload
property.addEventListener:
attachEvent
).removeEventListener()
which requires info identifying the original eventListener.If addEventListener
is supported and you only need one listener, then you can use either.
If it's a simple self-contained piece of code that nobody else will be messing with then, there's no issue with using onload
. If it's a more complex piece of software that other developers may mess with and any kind of extensibility is desired and you have cross browser support for event listeners, then addEventListener()
is more flexible and is probably more desirable.
image.addEventListener("load", function() {} );
will not work on all browsers, but assuming 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 benefit that you are not overriding any onload event that already exists on the image, and that your onload event will not be overridden by any other code.
Directly modifying the "onload" attribute of a DOM Element is usually not recommended just because you may think "Hey, I can save a couple lines of code by setting it, and I only need one listener, so I'll just set it rather than using addEventListener/attachEvent" - but it invariably leads to "Well what if I need to add something else later?"
For this reason javascript developers usually use a library to attach events, so you can add the listener confidently with one line of code and it will work in all browsers.
Both, Image.complete
as well as image.onload
don't seem to work in Webkit, at least the version on my mobile. They return false
infinitely. It works in opera though, so the code may be ok.
Therefore I am currently using a trick that works at least in opera and webkit. I have a 1x1 pixel placeholder image ready. Now I set the src
to the to be loaded image and (usually a bad practice, but this is a game that cannot start without that image) I go into a loop that updates some "loading" splashscreen gfx, but basicly just checks the width of the image, whether the image.width
is bigger than 1 pixel.
Like I said, tested only with opera and webkit. I use it to load an atlas image and copy the containing tiles to individual images over a canvas. Once finished, the src
of the atlas image is set back to the 1x1 placeholder, so it can be used that way again. For complete savity, one should also wait until the placeholder is 1 pixel in width again, before reuseing it that way.
const handleImage=()=>{}
image.addEventListener("load",handleImage); //adding a eventListener
image.removeEventListener("load",handleImage); //removeing a eventListener