Skip to content Skip to sidebar Skip to footer

Javascript : Passing A Function As An Argument

I want to load a default image if an image fails to load. I also want to call getLoadComplete() once the image has loaded. How do I pass the this.getLoadComplete() function into t

Solution 1:

img.onload = this.getLoadComplete();

You're invoking getLoadComplete() immediately, and assigning its return value to img.onload. You probably want this:

img.onload = this.getLoadComplete;

That is, set img.onload to the function itself, not its return value.

You don't need to do anything special in onerror; the onload handler will still be set to getLoadComplete, and when you modify the src in your onerror handler, it will invoke onload after the fallback image is loaded.


Solution 2:

you just need to call the function.

img.onload = this.getLoadComplete();

img.onerror = function(){
    img.src = "http://www.3d-maps-minus-3d.com/img/unavailable.gif";
this.getLoadComplete();
 }

Post a Comment for "Javascript : Passing A Function As An Argument"