Javascript image loading

Source: Internet
Author: User
Tags image flip

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.