Wrong image dimensions when it's dynamically loaded on a page the 1st time

Posted by Nikita Barsukov on Stack Overflow See other posts from Stack Overflow or by Nikita Barsukov
Published on 2010-04-10T09:17:24Z Indexed on 2010/04/10 9:23 UTC
Read the original article Hit count: 261

I have the following piece of Javascript code on my web-page

var central_image = document.createElement("img")
central_image.setAttribute("src", imgs[curr_image_no - 1]);
central_image.setAttribute("name", "jpeg");
document.getElementById("photo").appendChild(central_image);
central_image.onload =  getDimensions(); //function that alerts image dimensions

Now, when the central_image is loaded for the 1st time in Firefox, its height always equals to 0. In IE its dimensions are 28 x 30 pixels. When I reload image, its dimensions are OK in both browsers.

I guess the problem is that function getDimensions() starts before image was loaded completely. How to change that?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about web-development