?
Solved

How do I tell when an image has been fully loaded using javascript

Posted on 2009-07-09
8
Medium Priority
?
324 Views
Last Modified: 2012-05-07
Hi, I have a website with some pretty large background images. On the first page I want to preload the backgrounds for the other pages into the cache using javascript.
eg

<SCRIPT LANGUAGE = JAVASCRIPT>
   img1 = new Image();
   img2 = new Image();
   img1.src = "imageName1.gif";
   img2.src = "imageName2.gif"
</SCRIPT>
Is there a way in javascript of knowing when these images have all been downloaded?

Many thanks

Matt
0
Comment
Question by:Bigshowmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24816181
Yes there is. If you have an image with the name, say "myimage01" in your html code, you can access it's onLoad method as shown in the code:
<html>
<body>
<img name="myimage01" src="whatever.gif" width="100" height="50" onLoad="your script here">
</html>

Open in new window

0
 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24816212
Also, the image object also has an onLoad method so you could also use "img1.onload" in your code.
0
 

Author Comment

by:Bigshowmg
ID: 24816289
Thanks
IN RESPONSE TO YOUR FIRST POST
But the images that I'd be loading would not feature on the same page as the javascript. I just need to load them into the cache so other pages on the site don't take any time to download them. The first page will feature a loading image and I want this to disapear once the images have been downloaded.But the images that I'd be loading would not feature on the same page as the javascript. I just need to load them into the cache so other pages on the site don't take any time to download them. The first page will feature a loading image and I want this to disapear once the images have been downloaded.

AND THE SECOND
Could you give me an example of img1.onload? I assume it returns false until the image is loaded? Would I keep running a While loop until I get a TRUE or is there a less messy way?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24816420
Actually it's more like this:

img.onload = onLoadHandler;

Which is your function name.
0
 

Author Comment

by:Bigshowmg
ID: 24816485
So the function will automatically be called once the image has loaded? (sorry if I'm being obvious).
Also do you know if this works across all browsers?

Cheers

Matt
0
 
LVL 6

Expert Comment

by:ahmad2121
ID: 24816524
onLoad is an EVENT, therefore it executes when that "event" occurs. Somehow I do not think this is what you want.

Basically what you are trying to do is begin the downloading of the images as soon as someone enters the first page.

This you can accomplish by putting them in IMG tags and setting the display to none

<img src="mybackground.png" width="100px" height="100px" style="display: none">

The browser will load the image. If you are using PHP or ASP, you can point it to a folder and have it automatically generate that HTML.

Or you can create an array in Javascript with the image names in it, and write a function to generate their img code onload.
0
 
LVL 7

Accepted Solution

by:
nikhilmenon earned 2000 total points
ID: 24816529
Yes, the onload event would be triggered as soon as the image has loaded.

From my experience, this varies from browser to browser. Netscape (if still used) has trouble firing this. Also, some browsers do not fire this event if the image is being loaded from the cache. A possible bug with IE can be avoided by ensuring:

img.onload is specified BEFORE img.src tht is,


img.onload = onLoadHandler;
img.src = "foo.png";

Open in new window

0
 

Author Comment

by:Bigshowmg
ID: 24816576
Thats great, thanks for your help!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question