JavaScript replaces alt_javascript technique by using onerror to set default image display

Source: Internet
Author: User

JavaScript code

Processing function when an image load Error
errorimg (img) {
IMG.SRC = "default picture. jpg";
Img.onerror = null;
}

HTML code

 
 

In order to be beautiful when the Web page image does not exist when the fork picture is not displayed

When the page is displayed, if the picture is moved or lost, it will display a picture with X on the page, which affects the user's experience. Even if you use the ALT attribute to give the "picture XX" hint information, also can not play a big role.
In fact, you can do this: when the picture does not exist, will trigger the OnError event, we can do a remedial work in the event, such as:

1, let this picture element is hidden:

In order to be beautiful when the Web page does not exist when the picture does not display fork picture src= "Picture URL address" alt= "Picture xx" onerror= "this.style.display= ' None '"/>

2, with the default picture replacement:

In order to be beautiful when the Web page does not exist when the picture does not display fork picture src= "Picture URL address" alt= "Picture xx" onerror= "this.src= ' default picture URL address '"/>

Note: If used improperly, it can cause a dead loop in the IE kernel browser. For example: When the "Default picture URL address" also loaded unsuccessful (such as slow speed) or non-existent, it will be repeated loading, resulting in stack overflow error.

Therefore, you need to address the following two ways:

A, change the onerror code for other processing or ensure that the default picture in OnError is small enough and exists.

B, Control onerror event only trigger once, need to add this sentence: This.onerror=null; The increase is as follows:

In order to be beautiful when the Web page does not exist when the picture does not display fork picture src= "Picture URL address" alt= "Picture xx" onerror= "this.src= ' default picture URL address; This.onerror=null '"/>

After testing, the above method in all versions of IE and Google, Firefox browser support.

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.