HTML: Creating a Default Placeholder for Image that Fails to Load

PROBLEM

Let’s assume our web application relies on an external system to provide the image link, for example: employee photo link of a company.

<img src="http://external/server/employeeId.jpg"/>

If the image link is invalid, we will get the ugly looking “x” on certain browsers.

SOLUTION

To fix this, we can load an image placeholder from our own web application if the external image link fails to load for some reason:-

<img src="http://external/server/employeeId.jpg" 
     onerror="this.src='http://server/app/resources/img/avatar.jpg'"/>										

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s