Solved

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

Posted on 2009-07-09
8
318 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
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:nikhilmenon
Comment Utility
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
Comment Utility
Also, the image object also has an onLoad method so you could also use "img1.onload" in your code.
0
 

Author Comment

by:Bigshowmg
Comment Utility
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
 
LVL 7

Expert Comment

by:nikhilmenon
Comment Utility
Actually it's more like this:

img.onload = onLoadHandler;

Which is your function name.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Bigshowmg
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thats great, thanks for your help!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now