john smith
john smith

Reputation: 13

image onload event not firing

Should this work?

<script type=text/javascript>

    function load_i () {
        img = new Image();
        img.onload = load_e();
        img.src = "whatever.jpg";
    }

    function load_e() { alert("loaded"); }

</script>

Right now the image gets loaded, but apparently the onload event isn't being triggered. Tried it in FF and Chrome.

Upvotes: 0

Views: 3912

Answers (1)

Alex
Alex

Reputation: 35409

change:

img.onload = load_e();

to:

img.onload = load_e;

With the following line:

img.onload = load_e();

You're calling load_e and assigning the result, undefined, to img.onload.

Upvotes: 11

Related Questions