Show default picture if picture doesn't exist

Source: Internet
Author: User

Sometimes we need to display the images in the database in batches, and the path of the pictures is of course stored in the database. However, if you find that some pictures do not exist, the Red Cross on the Web page will be very ugly, this time we can choose to display the default image instead, this will appear very humane. Hefeng County Tobacco Monopoly Bureau

The image object's OnError event is used to determine that an error replaces the image object's SRC as the default image URL.

JavaScript onerror Events

Using the OnError event is an old-fashioned standard method of capturing Javascript errors in a Web page.

The OnError event is generated whenever a script error occurs on the page. If you need to take advantage of the OnError event, you must create a function that handles the error. You can think of this function as the OnError event handler (onerror events handler). This event handler uses three parameters to invoke: Msg (error message), URL (the URL of the page where the error occurred), line (the lines of code where the error occurred).

Onerror=handleerrfunction Handleerr (msg,url,l) {//handle The error Herereturn true or false}

Whether the browser displays a standard error message depends on the return value of the onerror. If the return value is False, the error message is displayed in the console (JavaScript console). Conversely, it does not.

The following example shows how to use the OnError event to catch an error:

Show default picture if picture doesn't exist

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: 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.