There are cases in web designing wherein the image that you are placing are either unreachable or non-existing. This usually happens when image links that are hosted externally are dead or when images' src attribute are set to an invalid location.

Replace Broken Image with a Default One

For this cases, it would be best to replace an image with a default one. Here is a snippet for these matters:

(Note that we are using JQuery here, specifically the error handler)

$("img").error(function () {
    var defImg = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wMWCCsH+2lPuwAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAKoElEQVR42u3df0zU9QPH8deJeniA04XCH4QEmkXO5o7rh5luikud1iRAgqz5h26abZr+0Y/l+oHpplMHEUNN3BSWKTDn3cStVXBFI07TFjB/UMBwiqzmieePyKM/+vL53smdooIiPB+b20c+nw9++HzuyefzuTvfZ7JarZ0CENAQdgFAIACBAAQCEAhAIACBAAQCEAgw+Azt6YIul4u9hQEjKSmJMwjAJRZAIACBAAQCEAhAIACBAAQCgEAAAgEIBCAQgEAAAgEIBCAQgEAAEAhAIACBAAQCEAhAIACBAAQCEAhAIAAIBCAQgEAAAgEIBCAQgEAAAgEIBACBAAQCEAhAIACBAAQCEAhAIACBAAQCgEAAAgF6x9DB/MMfOHBAGzduDDr/3XffVWpqKo8SziAAAjFZrdbOnizocrkG9I5ISkoaND8r/I83ZxCAe5D7+1unoqJChYWFcjgccrvdevrpp/XBBx8oJiZGra2tKikpkdPpVFNTk0wmkxISEpSVlaWXXnrJ73vm5ubq1KlTamxslNvt1tWrVxUaGqro6GhNmjRJ6enpevLJJ/3W6ezsVGlpqUpKSvTHH38oPDxc48eP14IFC7RlyxZdvHjR2MawsDBjvTNnzqi4uFgul0ttbW0KCQlRXFyc5s6dq0WLFmnoUB4OXGLdwyWW7zLR0dE6f/683/y4uDjZbDaVlZXpn3/+Cfg91q9f7xfJ7U71Q4YM0aZNmzRjxgzja59++qkO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+aNHjxpxmM1mrV27Vg6HQ5WVlSooKOBAE0jfeZCjtPd0JPmvvvrKmF66dKkyMjIUFRUli8Uiq9XKQeQS6+7d7rWO/jRKe7CR5I8fP25MJycnc1A5g9w//WmU9mAjyfsOjt112QcCuS/udZT2a9eu9dq2BBtJPjQ0NGDQIJA+dzejtIeFhRnTLS0tfb6N8fHxxjTPWhHIfXU3o7THxsYa03l5eWppadHVq1f166+/6sMPP+z1bZw1a5YxvW3bNv3888/yeDxqaGjQRx99xEHkJr3v3M0o7ZmZmUYITqdTTqfTmPfYY4/1+jamp6fLbreroaFB586d04oVK4x5EyZM4CASSN+601Ha586dK6/Xq6KiIjU3NyskJESJiYl65ZVXNHv2bD377LO9un1ms1kFBQXKzc3V999/L4/Ho7i4OKWkpCglJUXPPfccBzHQPR2ju2MwYnR3gJt0gEAAAgEIBCAQgEAAAgEIBACBAAQCEAhAIACBAAQCEAhAIACBACAQgEAAAgEIBCAQgEAAAgEIBCAQgEAAEAhAIACBAAQCEAhAIACBAAQCEAgAAgEIBCAQgEAAAgEIBCAQgEAAAgEIBACBAAQCEAhAIACBAAQCEAgw0JisVmsnuwHgDAIQCEAgAIEABAIQCEAgAIEABALgf/4FGorFw1Sst8gAAAAASUVORK5CYII=";
    $(this).attr("src", defImg);
});

Here we are catching the error event and simply replace it with a data image URI.

Why use image URI?

Well, you can always use another image here. Just be sure that you unbind the error handler event in case that the image you are trying to replace is not reachable too. Unbinding it may cause an infinite loop inside the function and I don't think that it may result to something nice.

By using an image URI, we can assure that the image is actually existing, in the expense that the image is loaded on the page (unless you save it on an external JS file) every now and then, which technically increases the page size and load speed.

To summarize it, it's basically your choice! :)

Usage

Just add the snippet on every page and this will automatically replace all broken images on your page. I prefer to add this on an external page and call it at the end of the page.


1 comment :

  1. Based from our test it does in IE9, not sure with other version of IE. I think the plugin developer can answer it more precisely.

    ReplyDelete