Remove Broken Images Using MooTools or jQuery
A while back I wrote a post called Send Email Notifications for Broken Images Using MooTools AJAX. Looking back on that post, I failed to address the image itself. After some thought I've decided it would be best to remove the broken image from the page all together. Below you'll find how to do so using MooTools or jQuery.
The MooTools JavaScript
$$('img').addEvent('error',function() {
this.dispose();
});
The MooTools way of removing elements from the page is Element.dispose().
The jQuery JavaScript
$('img').error(function() {
$(this).remove();
});
The jQuery way of removing elements from the page is jQuery.remove().
Removing a broken image is a great way of preventing the user from seeing a shortcoming in your website.
![Conquering Impostor Syndrome]()
Two years ago I documented my struggles with Imposter Syndrome and the response was immense. I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions. I've even caught myself reading the post...
![CSS vs. JS Animation: Which is Faster?]()
How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps?
This article serves as a point-by-point...
![Event Delegation with MooTools]()
Events play a huge role in JavaScript. I can't name one website I've created in the past two years that hasn't used JavaScript event handling on some level. Ask yourself: how often do I inject elements into the DOM and not add an...
![Styling CSS Print Page Breaks]()
It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...
This trick does not seem to work on IE7, at least not for me.
I love it! Thanks!
Neat simple trick. The only thing i would argue, and this really comes down to everyones individual situation, is that instead of removing the image, I would replace it with a default/error image.
Looking for this – thank you
nunage.com
is ALL broken image. You will notice it on the first page.
.
To avoid broken images in wordpress I suggest using the Hot Linked Image Cacher plugin which will cache all hotlinked images in your uploads folder and relink the images to the original source. I run this every couple of posts to keep from having broken images in the first place.
@Anton: Funny you should mention that:
MooTools – http://davidwalsh.name/custom-missing-image
jQuery – http://davidwalsh.name/custom-missing-image-jquery
jeah – nice, i like the custom-image thing better but thats fine too.
but maybe with a big rich DOM this could be stressing for the browser (especially shitty ones like IEs)?
@David Walsh: I really like this :)
Instead of remove it , i’ll replace it with a default / error images :)
Would you recommend waiting to use the script until after the dom is ready?