The default image is displayed if the image does not exist.

Source: Internet
Author: User

Sometimes we need to display images in the database in batches, and save the images in the database as the path. However, if you find that some images do not exist, the Red Cross on the webpage will look ugly. At this time, you can choose to display the default image instead, which will be very user-friendly.

The onerror event of the image object is used to determine whether to change the src of the image object to the URL of the default image.

  
JavaScript onerror event

Onerror is an old-fashioned standard method for capturing Javascript errors on webpages.

As long as a script error occurs on the page, an onerror event is generated. To use the onerror event, you must create a function to handle the error. You can call this function onerror event handler ). This event processor uses three parameters to call: msg (error message), url (url of the page where an error occurs), and line (line where an error occurs ).

onerror=handleErrfunction handleErr(msg,url,l){//Handle the error herereturn true or false}

Whether the browser displays standard error messages depends on the onerror return value. If the returned value is false, an error message is displayed on the console (JavaScript console. Otherwise, no.

The following example shows how to use the onerror event to capture errors:

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.