On 1 Dec 98, Jack Killpatrick wrote:

> Only reason I didn't suggest it is because I didn't know it was an
> option. took a look at my JavaScript reference and found this for
> onLoad: supported by "'window' and 'image' in JavaScript 1.1". I
> couldn't find anything in my reference that gave me a list of what you
> could use inside <body>. Do you have the lowdown?  

Whew, I was afraid there was some "gotchya" to using onLoad that I 
didn't know about :)

As for what I *do* know about it... onLoad is simply an event handler like 
any other.  You use it to make the execution of a function (or applet) 
dependent on various objects being fully loaded: a window, a frameset, an 
image, or a layer.  

This has obvious implications for preventing errors in routines that require 
a given object to exist before they'll work correctly. It can also prevent 
similar errors in Java applets: "don't try to run the applet until the 
document/frameset is completely loaded."  The onLoad handler is placed in 
either the <body> or the <frameset> tag, depending on your context.  

In the specific case of pre-caching images for rollovers, the onLoad 
handler isn't strictly necessary -- by definition the use of "new Image()" to 
define the images will force the browser to place them in memory so 
they're available for the rollovers, whether or not the caching routine is 
actually placed in a named function called by onLoad.  

So the fix you suggested to Suz would certainly work, and wouldn't likely 
causes any errors.  But because the pre-loading routine was already 
written as a function in her code (albeit one that wasn't actually called 
from anywhere), I thought it was tidier to use onLoad to execute it, rather 
than to make it a "not-function" (erk.)

For those who want to use image pre-loading for rollovers, here's a 
generic script to do so. It combines the pre-loading and the actual rollover 
code, plus a routine to check if the images are already cached (thus 
preventing the caching from being done every time you reload the page):


        <!--Image preload and rollover script-->

<SCRIPT language="Javascript">

<!--
      var flag = false;

      function imageLoad(){     // called with onLoad in BODY tag

          if (document.images){

// list the on images
image1on = new Image(); image1on.src = "first-image-on.gif";
image2on = new Image(); image2on.src = "second-image-on.gif";

      return (flag = true);  
          }
      }

          if (document.images){

// list the off images
image1off = new Image(); image1off.src = "first-image-off.gif";
image2off = new Image(); image2off.src = "second-image-off.gif";

          }

      function turnOn(imgName){
          if (document.images && (flag) == true){
             document[imgName].src = eval(imgName + "on.src");
             }
       }

       function turnOff(imgName){ 
          if (document.images){
             document[imgName].src = eval(imgName + "off.src");
             }
       }

// -->
</SCRIPT>

[snip]

<body onLoad='imageLoad()'>

[snip]

<a      href="blah.html"
        onMouseOver="turnOn('image1')"
        onMouseOut="turnOff('image1)">
<img src="first-image-off.gif" width=100 height=20 border=0 
name="image1" alt="Foo"></a>
-----------
Brent Eades, Almonte, Ontario
   E-mail: [EMAIL PROTECTED]
           [EMAIL PROTECTED]
   Town of Almonte site: http://www.almonte.com/
   Business site: http://www.federalweb.com

____________________________________________________________________
--------------------------------------------------------------------
 Join The Web Consultants Association :  Register on our web site Now
Web Consultants Web Site : http://just4u.com/webconsultants
If you lose the instructions All subscription/unsubscribing can be done
directly from our website for all our lists.
---------------------------------------------------------------------

Reply via email to