[Javascript] Image PreLoad

Flávio Gomes flavio at economisa.com.br
Thu Sep 21 14:51:54 CDT 2006


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)

 

Nick Fitzsimons escreveu:

> On 21 Sep 2006, at 19:38, Flávio Gomes wrote:
>
>> myPreload = document.createElement("img");
>>
>> //myPreload.src = "http://www.google.com/intl/en/images/logo.gif";
>>
>> // A couple of large images to test
>>
>> //myPreload.src = "http://www.plantzafrica.com/vegetation/vegimages/ 
>> funderstoreyzululand.jpg";
>>
>> myPreload.src = "http://www.forestserviceni.gov.uk/our_forests/ 
>> forests/east%20district/images/belvoir/belvoir_1.jpg";
>>
>> myPreload.onload = function() { alert(" I'm Loaded!  
>> ");               //Fires on onload
>>                                document.body.appendChild (myPreload); 
>> //Append the image to the document
>>                                alert(" Here I am!  ");                
>> //Says "Hello"? =)
>>                                this.onload =  "";                     
>> // BUG FIX HERE
>>                              };
>>
>>
>>
>> People please test this code for me?
>> On my Mozilla the onload event kept on spamming all the time...
>
>
> Hi,
>
> The following worked for me on Safari 2.0.4, Firefox 1.5 Mac, Opera 9  
> Mac, Firefox 1.5 Windows, and IE 6 Windows:
>
> var img = document.createElement("img");
> img.onerror = function(e) {
>     // this event on the img element is apparently not supported by 
> Safari
>     alert("error loading image");
> }   
> img.onload = function(e) {
>     alert("image loaded");
> }
> img.setAttribute("src", "/static/images/bullet.gif");
>
> Not sure what your problem is (I find it unlikely that your onload  
> handler is sending unsolicited commercial email) but note that your  
> image onload handler is attempting to append the image to the body,  
> and it's entirely possible that the body doesn't exist in the DOM at  
> that point in time; that would give you an error. If you mean that  
> it's giving an error message in the JS Console, posting that message  
> might help.
>
> Cheers,
>
> Nick.


-- 
Flavio Gomes
flavio at economisa.com.br




More information about the Javascript mailing list