Create an Image object:var a=new image (); Defines the src: a.src= "Xxx.gif" of the image object; This is equivalent to caching a picture for the browser.
Image object:
Build Image object: Image Object name =new images ([width],[height])
Properties of the Image object: Border Complete height hspace lowsrc name src vspace width
Events for Image objects: Onabort onerror onkeydown onkeypress onkeyup OnLoad
It is important to note that the SRC attribute must be written to the back of the onload, or the program will fail in IE.
Reference code:
var img=new Image (); Img.onload=function () {Alert ("IMG is loaded")}; Img.onerror=function () {alert ("error!")}; Img.src= "Http://www.abaonet.com/img.gif"; Function Show () {alert ("body is Loaded");}; Window.onload=show;
After running the above code, testing in different browsers, found that IE and FF are different, in the FF, the loading of the IMG object is contained in the body
During the loading process, the body is loaded and the Window.onload event is triggered after the IMG load is complete.
In IE, the loading of an IMG object is not included in the body loading process, the body is loaded, and when the Window.onload event is triggered, the img
The object may not have finished loading, and the Img.onload event will fire after Window.onload.
Based on the above problem, consider the browser compatibility and page load time, try not to put too many images in the image object, otherwise under the FF
will affect the download speed of the page. Of course, if you do a pre-load function after window.onload, there will be no FF problems.
You can detect whether an image is loaded by using the complete property of the image object (each image object has a completed property when the image is in
When loading, the property value is false, and after any event in onload, onerror, or onabort occurs, the image loading process is completed (regardless of
Not successful), at which time the complete property is true)
var img = new Image (); IMG.SRC = OIMG[0].SRC = This.src.replace (/small/, "big"); ODiv.style.display = "block"; Img.complete? ODiv.style.display = "None": (Oimg[0].onload = function () {ODiv.style.display = "none"})
Note:
The onload event of the image object is supported by popular browsers such as IE Firefox.
IE8 and below, Opera does not support onerror Events
JS's new image