views:

25

answers:

2

Hello, I have the following simple preloading function which substitute an image "src" attribute with another image (an animated GIF "Loading"). The problem arises only in IE: if the "loading" GIF is smaller than the actual image src, that will be resized. For example if I have a square 100px image and preload it, the image is temporarly substituted by an animated GIF of 50x50px. Whem the original image is fully loaded it is NOT displayed at its size, but at the smaller 50px. Here is the code, if you need it

_preload = function(url, placeholderUrl) {
 var img = new Image();
 loading = true;
 var placeholder = new Element("img", {
  src: placeholderUrl
 });
 img.placeholder = placeholder;
 img.onload = function(evt) {
  this.placeholder.src = this.src;
  loading = false;
 }
 img.src = url;
 return placeholder;
}

alt text Here you can see the visual error

A: 

I guess replacing placeholder with img (the img-elements inside the dom), instead of simply changing the src-attribute of placeholder, should fix this issue.

Dr.Molle
This is also a solution, but it seems more difficult to me. Replacement in dom = addingNew + removingOld. Since I don't want to display 2 images at the same time, I'd have to remove() first, and then adding(), but adding could be rather a complex function if you no more have the placeholder element reference in the dom.
Raffaele
+2  A: 

You should be able to adjust the width/height of the image within the callback function:

img.onload = function(evt) {
  this.placeholder.src = this.src;
  this.placeholder.width = this.width;
  this.placeholder.height = this.height;
  loading = false;
}

Example: Resizing image onLoad

subhaze
This definetely solves my problem. Though, it's very strange that you are allowed to do something like this AND the browser still doesn't do it by itself :P
Raffaele
Setting the width/height is just like setting the src they're all public properties. But yes, it would be oh so nice if all the browsers worked the same with JS.
subhaze