?
Solved

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

Posted on 2009-07-09
8
Medium Priority
?
325 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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