Sometimes we need to display images in the database in batches, and save the images in the database as the path. However, if
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: