CD-110 Part II . JavaScript Tutorial (and set the image s border to zero) or attach a client-side image map to it. The combination of a link and image is how you make a clickable image button (the image type of form input element is not a scriptable object until IE4+ and NN6+). Interchangeable images The advantage of having a scriptable image object is that a script can change the image occupying the rectangular space already occupied by an image. In IE4+ and NN6+, the images can even change size, with surrounding content reflowing accordingly. The script behind this kind of image change is simple enough. All it entails is assigning a new URL to the image object s src property. The size of the image on the page is governed by the HEIGHTand WIDTH attributes set in the
tag as the page loads. The most common image rollovers use the same size image for each of the rollover states. In NN3 and NN4, the image can t change size on the page, which causes a differently sized replacement image to scale to fit the original dimensions. Precaching images Images often take several extra seconds to download from a Web server. If you design your page so an image changes in response to user action, you usually want the same fast response that users are accustomed to in multimedia programs. Making the user wait many seconds for an image to change can severely detract from enjoyment of the page. JavaScript comes to the rescue by enabling scripts to load images into the browser s memory cache without displaying the image, a technique called pre- caching images. The tactic that works best is to preload the image into the browser s image cache when the page initially loads. Users are less impatient for those few extra seconds as the main page loads than waiting for an image to down load in response to some mouse action. Precaching an image requires constructing an image object in memory. An image object created in memory differs in some respects from the document image object that you create with the
tag. Memory-only objects are created by script, and you don t see them on the page at all. But their presence in the document code forces the browser to load the images as the page loads. The object model provides an Imageobject constructor function to create the memory type of image object as follows: var myImage = new Image(width, height) Parameters to the constructor function are the pixel width and height of the image. These dimensions should match the
tag s WIDTHand HEIGHT attributes. Once the image object exists in memory, you can then assign a filename or URL to the src property of that image object: myImage.src = someArt.gif When the browser encounters a statement assigning a URL to an image object s src property, the browser goes out and loads that image into the image cache. All the user sees is some extra loading information in the status bar, as if another
In case you need quality webspace to host and run your web applications, try our personal web hosting services.