Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 23436  / 12 Years ago, thu, february 14, 2013, 12:00:00

For detecting when an image has completed downloading which method should I use?



image.onload = function () {}


or



image.addEventListener(load, function () {} );

More From » javascript

 Answers
318

onload:




  1. Supports only a single listener.

  2. Works in all browsers.

  3. You unbind the event handler by clearing the onload property.



addEventListener:




  1. Supports multiple listeners.

  2. Doesn't work in older IE browsers (they use attachEvent).

  3. You unbind the listener with 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.


[#80215] Wednesday, February 13, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandra

Total Points: 708
Total Questions: 100
Total Answers: 84

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
sandra questions
Tue, Jun 30, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Fri, May 31, 19, 00:00, 5 Years ago
;