Image ObjectNameImage Object---an embedded image in an HTML documentSynopsis
document.images[i] document.images.length document.image-name Constructor
new Image([width, height]); Arguments
ReturnsA newly created Image object. Properties
HTML SyntaxThe Image object is created with a standard HTML <IMG> tag, with the addition of event handlers. Some <IMG> attributes have been omitted from the syntax below, because they are not used by or accessible from JavaScript.
<IMG SRC="url" the image to display WIDTH=pixels the width of the image HEIGHT=pixels the height of the image [ NAME="image_name" ] a property name for the image [ LOWSRC="url" ] alternate low-resolution image [ BORDER=pixels ] width of image border [ HSPACE=pixels ] extra horizontal space around image [ VSPACE=pixels ] extra vertical space around image [ onLoad=handler ] invoked when image fully loaded [ onError=handler ] invoked if error in loading [ onAbort=handler ] invoked if user aborts load > DescriptionThe Image objects in the document.images[] array represent the images embedded in an HTML document using the <IMG> tag. Most of the properties of this object are currently read-only. The src and lowsrc properties are exceptions, however. Usually, the src (and perhaps lowsrc) properties are set once, when the Image object is created from HTML. But these properties may also be set dynamically. When you set the src property, the browser will load the image specified by the new value of the src property, or by the lowsrc property, if specified, for low-resolution monitors. Note that if you want to use the lowsrc property, you must set it before you set the src property, because setting the src property starts the download of the new image. You can dynamically create Image objects in your JavaScript code using the Image() constructor function. Note that this constructor method does not have an argument to specify the image to be loaded. As with images created from HTML, you tell Navigator to load an image by setting the src property of any images you create explicitly. There is no way to display an Image object in the web browser. All you can do is force the Image object to download an image by setting the src property. This is useful, however, because it loads an image into the browser's cache. Later, if that same image URL is specified for one of the images in the images[] array, it will be preloaded, and will be displayed quickly. You might do this with lines like the following:
document.images[2].src = preloaded_image.src; document.toggle_image.src = toggle_off.src; UsageSetting the src property of an Image object can be used to implement simple animations in your web pages. It is also an excellent technique for changing the graphics on a page as the user interacts with the page. For example, you might create your own "submit" button using an image and a hypertext link. The button would start out with a "disabled" graphic and remain that way until the user had correctly entered all the required information into the form, at which point the graphic would change, and the user would be able to submit the form. |
|