25.169. Image: an image in an HTML documentDOM Level 2 HTML: Node Element HTMLElement Input25.169.1. Constructornew Image(integer width, integer height) 25.169.1.1. Arguments
25.169.2. Properties
In addition to these properties, Image objects also support the following properties, which simply mirror HTML attributes:
25.169.3. Event HandlersImage inherits event handlers from HTMLElement and defines the following:
25.169.4. 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 commonly used in JavaScript: <img src="url" // The image to display width="pixels" // The width of the image height="pixels" // The height of the image alt="description" // Short description of image [ onload="handler" ] // Invoked when image is fully loaded [ onerror="handler" ] // Invoked if error in loading [ onabort="handler" ] // Invoked if user aborts load > 25.169.5. DescriptionAn Image object represents an image embedded in an HTML document with an <img> tag. The images that appear in a document are collected in the document.images[] array. Images that have name attributes can also be accessed through named properties of the Document object. For example: document.images[0] // The first image in the document document.banner // An image with name="banner" The src property of the Image object is the most interesting one. When you set this property, the browser loads and displays the image specified by the new value. This allows visual effects such as image rollovers and animations. See Chapter 22 for examples. You can create offscreen 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 created in this way; 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; if that same image URL is used later with an actual <img> tag, it will display quickly since it has already been loaded. 25.169.6. See AlsoChapter 22 |