Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Use Custom Missing Image Graphics Using jQuery

16 Responses »

Yesterday I posted an article about how you can use your own "missing image" graphics when an image fails to load using MooTools. Here's how to do the same using jQuery.

The jQuery JavaScript

$(document).ready(function() {
	/* version 1 */
	$('img.missing1').error(function() {
		$(this).attr({
			src: 'http://davidwalsh.name/dw-content/missing-image.jpg',
			alt: 'Sorry!  This image is not available!',
			style:'border: 1px solid #f00;width:110px;height:40px;'
		});
	});
	/* version 2 */
	$('img.missing2').error(function() {
		$(this).attr({
			src: 'http://davidwalsh.name/dw-content/missing-image-2.jpg',
			alt: 'Sorry!  This image is not available!',
			style:'border: 1px solid #f00;width:30px;height:28px;'
		});
	});
});

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!

Discussion

  1. April 3, 2009 @ 8:21 am

    Interesting :)

    There’s just one thing -> you wrote (The MooTools Javascript)

    Guess it should be ( The jQuery Javascript )

    MooTools runs in your blood man :D

  2. April 3, 2009 @ 8:26 am

    Oooooooops. Fixed.

    But MooTools DOES run in my blood! :)

  3. rakesh juyal
    April 3, 2009 @ 8:55 am

    So we can “Use Custom Missing Image Graphics using JQuery or MooTools”. :)

  4. April 6, 2009 @ 7:50 am

    Good job
    umm….attr

    $(this).attr({
    src: ‘http://davidwalsh.name/dw-content/missing-image.jpg’,
    alt: ‘Sorry! This image is not available!’,
    title: ‘Sorry! This image is not available!’,
    style:’border: 1px solid #f00;width:110px;height:40px;’
    });

    Support for firefox :P

  5. erik
    April 16, 2009 @ 5:43 am

    Do you know if it’s possible to use the .live function to bind this behavior?
    That would enable invalid dynamic images that are injected after the document.ready event to also have the same behavior..

    Besides that, thanks for a brilliant snippet..

    Cheers,

    Erik

  6. bassem
    May 7, 2009 @ 4:43 am

    But This means i have to give a “.missing” class to all images in my page ! ?? its not just a detection for the broken link

  7. May 7, 2009 @ 7:26 am

    @Bassem: You could automate giving that class to all of the images using jQuery.

  8. tilal husain
    June 12, 2009 @ 9:56 am

    I usually use jquery’s builtin function and it works perfectly

    $(“img”).error(function () {
    $(this).unbind(“error”).attr(“src”, “images/noimage.jpg”);
    });

  9. June 20, 2009 @ 11:45 am

    Hi I’m using this plugin and it’s working very well except for one problem. It seems to be interfering with the jQuery ui dialog feature. When I include $.idleTimer(120000) in my script firebug shows a ‘too much recursion’ error when I attempt to close an open modal dialog. When I remove that line of code it works fine. Any ideas? Thanks in advance

  10. rp
    February 19, 2010 @ 12:49 pm

    Very cool. Just what I needed. Thanks!!!!!!!!!!!!!!!!!!!!

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!