Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2003, 08:01 PM   PM User | #1
Josh Campbell
New Coder

 
Join Date: Jun 2002
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Josh Campbell is an unknown quantity at this point
Image Preloading

I am using javascript to display random images. Whenever I load the page it shows broken images and then it loads the picture. My script is:

Code:
<script>
var Top = new Array("images/top_1.gif","images/top_2.gif","images/top_3.gif","images/top_4.gif");
var Bottom = new Array("images/bottom_1.gif","images/bottom_2.gif","images/bottom_3.gif","images/bottom_4.gif");
imgCt = Top.length;
function choosePictures()
{
if(document.images)
{
randomNum = Math.floor((Math.random() * imgCt));
document.imgTop.src = Top[randomNum];
document.imgBottom.src = Bottom[randomNum];
}
}
 </script>
My images look like this:
<img src="" name="imgTop" width="770" height="103" />
<img src="" name="imgBottom" width="770" height="25" />

The choosePictures() function is called in the body tag onload. If I set the src for both imgs to the first images it shows the first images and then about a second later loads the new random image. Is there a way to tell the browser to wait and display the page only whenever the images are done loading? I've searched javascript site but I don't understand alot of what they write. Thanks
Josh Campbell is offline   Reply With Quote
Old 01-18-2003, 11:01 PM   PM User | #2
Algorithm
Regular Coder

 
Join Date: Jul 2002
Location: USA
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Algorithm is an unknown quantity at this point
If you give img tags a blank src, they will display a broken image. Instead, give them the src for a transparent 1x1 image, which can then be replaced.

<img src="blank.gif" name="imgTop" width="770" height="103" />
Algorithm is offline   Reply With Quote
Old 01-19-2003, 02:53 AM   PM User | #3
Josh Campbell
New Coder

 
Join Date: Jun 2002
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Josh Campbell is an unknown quantity at this point
duh! how dumb of me. I should have thought of that. Thanks algorithm.
Josh Campbell is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:12 AM.


Advertisement
Log in to turn off these ads.