[Javascript] Image PreLoad
Nick Fitzsimons
nick at nickfitz.co.uk
Thu Sep 21 14:11:57 CDT 2006
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.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/
More information about the Javascript
mailing list