preloading a image

JavaScript related questions should go here.
Post Reply
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

preloading a image

Post 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.
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: preloading a image

Post 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';
Image
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: preloading a image

Post 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
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: preloading a image

Post by jacek »

ta2shop wrote:so just adding the head part of the code will solve my problem?
Well it will start the image downloading.
Image
Post Reply