During website development, some page effects (such as image flip) require images to be displayed immediately. Some page images are too many and the download time is relatively long, you must display other images (indicating that the image is being downloaded) before downloading the image.
To achieve these effects, image objects in JavaScript are generally used.
-
-
Main attributes of an image object
-
-
-
SRC
-
Image address.
-
Complete
-
Indicates whether the image is cached by the browser or has been loaded to the current page.
This attribute is a little different in IE. in IE, the complete attribute is changed only after the onload event, while in Firefox, opera, and other browsers, it has changed before onload. That is CodeWhen the image can be downloaded normally and the image is not cached by the browser, the results executed by IE and Firefox, opera, and other browsers are different.
VaR testimage = new image (); testimage. src = "http://www.olnote.com/images/logo.gif"; testimage. onload = function () {alert (testimage. complete); // if the preceding conditions are met, ie will display "false" while Firefox, opera, and other browsers will display "true ".};
-
-
Main events of the image object
-
-
-
Onload
-
Triggered when the image is downloaded.
There are also differences between IE and Firefox, opera, and other browsers about this event. If the image has been cached by the browser or downloaded to the current page, ie will not trigger this event.
The following is an example of loading an image first and then loading the same image again. (Note: Do not set the image to browser cache here)
VaR url = "http://www.olnote.com/images/logo.gif"; var LoadImage = new image (); LoadImage. src = URL; LoadImage. onload = function () {alert ("onload event triggered"); var againloadimage = new image (); againloadimage. src = URL; againloadimage. onload = function () {// because the image has been loaded to the current page, the onload event will not be triggered in IE. Alert ("onload event triggered again ");}}
-
Onerror
-
Triggered when the image download fails.
-
-
Javaascript image loading example (browser compatibility)
-
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
-
-
Source: http://www.olnote.com/itlife/note/100000069.aspx