[Javascript] Image PreLoad

Nick Fitzsimons nick at nickfitz.co.uk
Thu Sep 21 15:23:05 CDT 2006


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/






More information about the Javascript mailing list