1
var icon1 = new Image();
var wid1;
var hei1;
icon1.src = resource;                   // resource is Image URL here
icon1.onload = function () {
    wid1 = icon1.width;
    hei1 = icon1.height;
    var rati = wid1 / hei1;
};

This code is to calculate the ratio of width and height to further maintain the aspect ratio of the image but it doesn't work at all on IE. is there anything else that i can use for maintaining the aspect ratio of image that will work on all browsers?

user1535672
  • 193
  • 3
  • 12
  • Works fine for me in IE: http://jsfiddle.net/9bDxt/1/ – j08691 Jul 20 '12 at 16:10
  • 1
    I would put the code that sets the "src" attribute *after* the code that sets up the "onload" handler. Not necessary for good browsers, but I have vague memories that if the image is found in the cache (by IE), the handler might not be called. – Pointy Jul 20 '12 at 16:10
  • Is it possible the image is in the cache? I read somewhere that onload doesn't fire when the image comes from the cache. – carny666 Jul 20 '12 at 16:11
  • 1
    http://stackoverflow.com/a/5933319/315721 – carny666 Jul 20 '12 at 16:12
  • possible duplicate of [Javascript Image onLoad](http://stackoverflow.com/questions/5933230/javascript-image-onload) – Andrew Leach Jul 20 '12 at 17:57

0 Answers0