AvailabilityJavaScript 1.1 Inherits from/OverridesInherits from HTMLElement Synopsisdocument.images[i] document.images.length document.image-name Constructornew Image(width, height) Arguments
PropertiesImage inherits properties from HTMLElement and defines the following properties, most of which correspond to the HTML attributes of the <img> tag. In JavaScript 1.1 and later, the src and lowsrc properties are read/write and may be set to change the displayed image. In browsers that do not allow document reflow, such as IE 3 and Netscape 4, the other properties are read-only.
Event HandlersImage inherits event handlers from HTMLElement and defines the following:
HTML SyntaxThe Image object is created with a standard HTML <img> tag. Some <img> attributes have been omitted from the following syntax 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 is 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. The src property is the most interesting one; when you set this property, the browser loads and displays the image specified by the new value. You can create Image objects dynamically 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 the browser 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 is preloaded and displays quickly. You can do this with the following lines: document.images[2].src = preloaded_image.src; document.toggle_image.src = toggle_off.src; UsageSetting the src property of an Image object is a way 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 can create your own Submit button using an image and a hypertext link. The button will start out with a disabled graphic and remain that way until the user correctly enters all the required information into the form, at which point the graphic changes, and the user is able to submit the form. |