2

Possible Duplicate:
jQuery or JavaScript: Determine when image finished loading

The problem is known, but I can't find any simple solution:

var img = new Image();

img.onLoad = function() {
  console.log("load");         // this event doesn't fire
};

img.src = "img/domino/21.png";

console.log(img.complete);     // false

setTimeout(function() {
  console.log(img.complete);   // sometimes true, sometimes false
}, 10);

I was looking for an implementation of onComplete event, but I can't find anything. Would you help?

Community
  • 1
  • 1
ciembor
  • 6,909
  • 11
  • 53
  • 96

5 Answers5

7

The proper spelling of the event handler property is all lowercase .onload, not .onLoad.

var img = new Image();

img.onload = function() {
  console.log("load");         // works every time
};

img.src = "img/domino/21.png";

Javascript is case sensitive so you MUST use the proper case for all object properties.


The alternatives besides .onload are to use:

img.addEventListener("load", function(e) {...}); 

or (in older versions of IE):

img.attachEvent("onload", function(e) {...});

If you're only using one event handler and you aren't using a cross platform library that already abstracts event handlers for you, then using .onload is the simplest.

And, here's a simple cross browser way to add event handlers:

// add event cross browser
function addEvent(elem, event, fn) {
    if (elem.addEventListener) {
        elem.addEventListener(event, fn, false);
    } else {
        elem.attachEvent("on" + event, function() {
            // set the this pointer same as addEventListener when fn is called
            return(fn.call(elem, window.event));   
        });
    }
}
jfriend00
  • 637,040
  • 88
  • 896
  • 906
5
var img = new Image();
img.onload = function() {
    console.log("load");
};
img.src = "img/domino/21.png";
console.log(img.complete);     // false
setTimeout(function() {
  console.log(img.complete);   // sometimes true, sometimes false
}, 10);

In Chrome,Firefox and Safari

var img = new Image();
img.addEventListener('load',function() {
    console.log("load");
});
img.src = "img/domino/21.png";
console.log(img.complete);     // false
setTimeout(function() {
  console.log(img.complete);   // sometimes true, sometimes false
}, 10);

Or in IE or Opera

var img = new Image();
img.attachEvent('onload',function() {
    console.log("load");
});
img.src = "img/domino/21.png";
console.log(img.complete);     // false
setTimeout(function() {
  console.log(img.complete);   // sometimes true, sometimes false
}, 10);
Danilo Valente
  • 11,037
  • 8
  • 52
  • 66
1

Are you sure your image at img/domino/21.png exists? The following code works for me in Firebug's console.

var img = new Image();

var onload = function() {
    console.log('load');
}

img.onload = onload;

img.src = "http://img.gawkerassets.com/img/17m7otdiw6n8fjpg/original.jpg?" + (new Date()).getMilliseconds();
Michael Robinson
  • 28,575
  • 12
  • 103
  • 128
1

If you can use jQuery it can be simple:

$('img').load(foo).error(foo);

Or with vanilla javascript:

img.onload = img.onerror = foo;
gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
0
var img = new Image();

//blah blah...

img.addEventListener("load",function() {
    console.log("Loaded!");
});
Derek 朕會功夫
  • 88,688
  • 41
  • 174
  • 241