0

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 the function called on img.onerror?

Here is the code I've got:

    img.onload = this.getLoadComplete();

    img.onerror = function(){
        img.src = "http://xxxx.com/image.gif";
     }

     img.src = request.url;
  • 1
    possible duplicate of [javascript pass function as parameter](http://stackoverflow.com/questions/13286233/javascript-pass-function-as-parameter) – Evan Oct 28 '13 at 19:35
  • Does `getLoadComplete` return a function or is it the actual function you would like to call? – James Montagne Oct 28 '13 at 19:40

2 Answers2

3
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.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

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();
 }
André Leal
  • 186
  • 1
  • 8