JS's new image

Source: Internet
Author: User

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

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.