PHP, MYSQL, CSS, LINUX TUTORIALS

Add error image when a image is not found

When creating a website, a priority is to make your website load faster. That’s good!
But what are you doing if your website is embeding some images who doesn’t exist?

Example:

View Code HTML
<img src="http://example.com/my-image.jpg">

If the image (my-image.jpg in our example) doesn’t exist, your website will get loaded more slowly but if you user the .error event from jQuery you can solve this problem easily.

Example:

View Code JAVASCRIPT
$(document).ready(function() {
	$('img').error(function(){
		$(this).attr('src', 'http://example.com/image-not-found.jpg);
	});
});

So this script changes the image source to the error image (image-not-found.jpg) in our example) and your should not have any problem visiting your website.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>