[Javascript] Image PreLoad

Terry Riegel riegel at clearimageonline.com
Fri Sep 22 06:55:37 CDT 2006


Hello,

Thanks for the answer. I found that in my application the onload  
event would misfire periodically, so I set up a little bit more code  
to make sure I knew the image was in. I found IsImageOk and modified  
it for my needs. When calling loadImage() it will see if the image is  
already loaded and display. If it isn't loaded it will load it with  
an onload event firing the display routine. If the onload event  
doesn't trigger them it will check back every second to try again.

Any suggestions on improving this code would be helpful.

Terry


function IsImageOk(img)
  {
   if (!img.complete)
    { return false; }
   if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    { return false; }
   return true;
  }

function loadImage(src)
{
   var i=new Image;
   i.src=src;
   i.onload=showit;
   if (IsImageOk(i))
    { showit(); }
   else
    { spinneron();
      setTimeout('loadImage(src)',1000); }
}




On Sep 21, 2006, at 4:23 PM, Nick Fitzsimons wrote:

> On 21 Sep 2006, at 20:51, Flávio Gomes wrote:
>
>> heheheh, no, it's not sending mail =)
>>
>> There's no error message, and the "document.body" already exists.
>>
>> What I believe to be the problem is that I set a call to  
>> "document.body.appendChild(img)" (...)
>>
>> ===============
>> img.onload = function(e) {
>>    alert("image loaded");
>>    document.body.appendChild(this);
>> }
>> ===============
>>
>> (..)
>> inside the "onload handler" and, on Mozilla it seems to call the  
>> "onload" when a object is appended, making my code get locked in  
>> an infinite loop.
>>
>> Can you test this code for us on your Firefoxes and Safari? (It  
>> doesnt loop in IE6 Windows)
>>
>
> Hi,
>
> I don't have any problem even when appending the image to the body  
> on Firefox 1.5.0.7 (Mac & Win), Opera 9 (Mac), Safari 2.0.4, or IE  
> 6; I've put up a test page at:
>
> <http://www.nickfitz.co.uk/imageload.html>
>
> with the script that's working for me in the page. It's not much  
> (if at all) different to your code. What version of Firefox are you  
> having problems with? If it's the same one, I'm not sure why it's  
> not working.
>
> One minor difference between my and your code is that you set the  
> "src" property before attaching the onload handler, but when I  
> changed my code to do the same, it still worked. So that's one  
> potential issue you don't have to check :-) Oh, and it doesn't seem  
> to make a difference if one uses my approach of setting the image  
> source using setAttribute, or yours of setting the src property  
> directly - still works.
>
> Cheers,
>
> Nick.
> -- 
> Nick Fitzsimons
> http://www.nickfitz.co.uk/
>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list