"JavaScript" Loading failed Red Cross image processing with <img> picture label OnError Event

Source: Internet
Author: User

You must have seen the Internet when the load failed red Fork picture, specific as. Of course, Google Chrome and Firefox will look good in their processing now.


You can actually take advantage of the onerror event of the picture tag to handle it, requiring it to load a default picture immediately after it fails, instead of displaying it as a red fork.

The code is as follows:

Meaning, if loading s.png this picture fails, immediately to load xx.png this picture, simultaneously xx.png this picture requests it to load in 80x80 way.

Of course, if you want to set more properties, you can write it exactly like this:

The JavaScript handler function that declares that the s.png fails to load is onerrorhandle, and then passes itself, that is, the IMG node,

Then write one of these onerrorhandle functions in javascript:

function Onerrorhandle (obj) {obj.src= "xx.png"; obj.width=80;obj.height=80;}
In this case, you can set more properties on this onerrorhandle function and use the Obj.xx property on it. At the same time avoid the onerror= "" in the double quotation marks of the event to write too many single quotes, to stun themselves embarrassed.

"JavaScript" Loading failed Red Cross image processing with picture label OnError Event

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.