Use Custom Missing Image Graphics Using MooTools

By  on  

Missing images on your website can make you or your business look completely amateur. Unfortunately sometimes an image gets deleted or corrupted without your knowledge. You'd agree with me that IE's default "red x" icon looks awful, so why not use your own missing image graphic?

The MooTools JavaScript

window.addEvent('domready',function() {
	/* version 1 */
	$$('img.missing1').addEvent('error',function() {
		this.set({
			src: 'https://davidwalsh.name/demo/missing-image.jpg',
			alt: 'Sorry!  This image is not available!',
			styles: {
				width: 110,
				height:40
			}
		});
	});
	/* version 2 */
	$$('img.missing2').addEvent('error',function() {
		this.set({
			src: 'https://davidwalsh.name/demo/missing-image-2.jpg',
			alt: 'Sorry!  This image is not available!',
			styles: {
				width: 30,
				height:28
			}
		});
	});
});

Note that I've provided two examples. If you want to get really specific, you can create multiple images and account for different sizes when possible.

A great website accounts for all of the details. This is yet another way of accounting for the finest of details. A website is NEVER complete!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    MooTools Star Ratings with MooStarRating

    I've said it over and over but I'll say it again:  JavaScript's main role in web applications is to enhance otherwise boring, static functionality provided by the browser.  One perfect example of this is the Javascript/AJAX-powered star rating systems that have become popular over the...

  • By
    MooTools & Printing – Creating a Links Table of Contents

    One detail we sometimes forget when considering print for websites is that the user cannot see the URLs of links when the page prints. While showing link URLs isn't always important, some websites could greatly benefit from doing so. This tutorial will show you...

Discussion

  1. This is very smart. great job

  2. not bad…

  3. Nice idea. There should be a server side method of doing this – something with .htaccess I guess. Let me go search for that.

  4. Is there a way to track missing images for div that use images as their background?

  5. @Jesus DeLaTorre: I suppose you could use MooTools’ Assets plugin to load it in initially to catch the error, but not from a stylesheet.

  6. chris

    shouldn’t you rather have it email you and tell you that the image is missing?

  7. @David Thanks for the heads up. I’ll see about integrating the too.

    @Chris I think David’s solution is valid. You can still grep the logs and see if the missing image file is ever requested and then all you have to do check the referer. At the bare minimum, you have to at least provide a user friendly image and then you can add something like email or an ajax call to log the error.

  8. I dont quite understand how this works. Are you supposed to id the broken images by giving them a special hook (class name, alt tag, etc.)? If you can do that, why not fix the image?

  9. My bad, I didnt notice the error event

  10. This could be done server side using PHP’s “file_exists” function. Still I like your creativity with this script.

  11. @Doug Waltman: There are other types of image errors, like a corrupt image which file_exists() wouldn’t prevent.

  12. Very useful tip!!

  13. very useful thing!
    thx for the tip.

  14. BeN

    @Doug Waltman: Using server side script (file_exists, file_get_contents) is just one method to check broken images and is very usefull, i am working on a flickr app … Some firewalls and some intranets doesnt allow connection to flickr (or youtube, and other services). In that cases the only way to fix that problem is using js on the client side.

  15. Hello David and sorry for my English.

    Does it work with mootools-release-1.11 ?

    Thanks for your answer.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!