Solved

use onLoad to hide/show images

Posted on 2001-08-06
5
495 Views
Last Modified: 2008-03-04
Hello,

I have an intranet site with GIF images which load slowly and at different rates.  I am looking for someone who can give me usable code which preloads images into cashe (possibly using the onLoad function and looping until every image is loaded and then displaying the images all at once.)  

I use the <DIV> tag to position my images and can set my images to be hidden by default instead of visible.   I am a novice web developer with no javascript experience so please keep everything simple.

Thank you!

0
Comment
Question by:stevenshl
5 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 6356011
Is it an IE only intranet?

If so you can use the following.

Add this to the <head>-section:
<style type='text/css'>
IMG{visibility:hidden;}
</style>

Then load the usual way (no changes needed in your current code). And then add the folowing script (also in the Head-section):
<script>
function document_onload()
{
   for(i=0;i<document.images.length;i++) document.images[i].style.visibility="visible";
}
window.onload=document_onload;
</script>



If you want a different approach, just ask. me or someone else will be able to help you out.

regards,
CJ
0
 
LVL 9

Accepted Solution

by:
nimaig earned 100 total points
ID: 6356187
Try this. Works in both IE and netscape

<html>
<script language="JavaScript">
<!--
     var imgCount = 9;
     var imgLoaded = 0;
     function dispDIV(){
          imgLoaded++;
          if(imgLoaded >= imgCount){
               if(document.all){
                    window.divImg.style.visibility="visible";
               }else if(document.layers){
                    document.divImg.visibility="show";
               }
          }
     }
//-->
</script>
<body>
<form name=myform>
</form>
<DIV name="divImg" id="divImg" style="position:absolute;visibility:hidden">
     <img src="http://otnxchange.experts-exchange.com/images/otn_logo_75x44_xp.gif" onLoad="javascript:dispDIV()">
     <img src="http://www.onjava.com/images/onjava/onjava_logo.jpg" onLoad="javascript:dispDIV()">
     <img src="http://careers.onjava.com/graphics/complogo_thomson.gif" onLoad="javascript:dispDIV()">
     <img src="http://m.doubleclick.net/viewad/567037-468-6to4.gif" onLoad="javascript:dispDIV()">
     <img src="http://m.doubleclick.net/viewad/538206-125-essentialjs2.gif" onLoad="javascript:dispDIV()">
     <img src="http://servedby.advertising.com/mnum=0000018953/logs=0/bins=1/site=0000034004/bnum=34004" onLoad="javascript:dispDIV()">
     <img src="http://otnxchange.experts-exchange.com/images/otn.gif" onLoad="javascript:dispDIV()">
     <img src="http://m.doubleclick.net/viewad/620465-altoweb_120x480_rev2__060501aaaa.gif" onLoad="javascript:dispDIV()">
     <img src="http://ad.doubleclick.net/557907/sgp2q01_336x280_WS_DEV_v_r3_c2.jpg" onLoad="javascript:dispDIV()">
</DIV>

</body>
</html>

0
 

Author Comment

by:stevenshl
ID: 6356646
Our intranet is used by IE and Netscape... we try to keep users current  -  within the last two revisions of both.
0
 
LVL 4

Expert Comment

by:ravi_raj123
ID: 6359165
HI,

<html>
<head>
<script language="JavaScript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000" background="images/Celebrations.jpg" onLoad="MM_preloadImages('images/pic0.jpg','images/pic1.jpg','images/pic2.jpg','images/pic3.jpg','images/pic4.jpg','images/pic5.jpg')">
</body>
</html>

Hope this is pretty clear to you.
Hope this helps you.
Good Luck...:)
0
 

Author Comment

by:stevenshl
ID: 6361277
Thanks for your help... this code definitely got me going in the right direction.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 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

12 Experts available now in Live!

Get 1:1 Help Now