Page 1 of 1

preloading a image

Posted: Sat May 21, 2011 8:48 am
by ta2shop
hy guys, i was searching for a javascript or somting to preload a image, an i found this, wich goes in the head of the page:
<SCRIPT language="JavaScript">
<!--
pic1= new Image(100,25); 
pic1.src="img/bg1.png"; 
//-->
</SCRIPT>
and this needs to go in the body part pf the page where the image is diplayed:
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
  pic1= new Image(100,25); 
  pic1.src="http://someplace.com/image1.gif"; 
}
//-->
</SCRIPT>
but my image is in the css, in the body tag, so how can i change this so that it will work, or how sould i added to the css?
thanks.

Re: preloading a image

Posted: Sat May 21, 2011 10:24 am
by jacek
Just doing
pic1= new Image(100,25);
pic1.src="img/bg1.png"; 
should start downloading the image, so you can use it where ever as you would normally do.

to change a background image with javascript
element.style.backgroundImage = 'file.png';

Re: preloading a image

Posted: Sat May 21, 2011 11:16 am
by ta2shop
so just adding the head part of the code will solve my problem?
even for the bg imge that is caled with css?
thanks

Re: preloading a image

Posted: Sat May 21, 2011 12:14 pm
by jacek
ta2shop wrote:so just adding the head part of the code will solve my problem?
Well it will start the image downloading.