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.