code_junkie

jQuery/Javascript to replace broken images

Nov 10th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. function ImgError(source){
  2. source.src = "/images/noimage.gif";
  3. source.onerror = "";
  4. return true;
  5. }
  6.  
  7. <img src="someimage.png" onerror="ImgError(this);"/>
  8.  
  9. $("img").error(function () {
  10. $(this).unbind("error").attr("src", "broken.gif");
  11. });
  12.  
  13. $('#images img').preload({
  14. placeholder:'placeholder.jpg',
  15. notFound:'notfound.jpg'
  16. });
  17.  
  18. $(window).load(function() {
  19. $('img').each(function() {
  20. if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
  21. // image was broken, replace with your new image
  22. this.src = 'http://www.tranism.com/weblog/images/broken_ipod.gif';
  23. }
  24. });
  25. });
  26.  
  27. $(window).bind('load', function() {
  28. $('img').each(function() {
  29. if((typeof this.naturalWidth != "undefined" &&
  30. this.naturalWidth == 0 )
  31. || this.readyState == 'uninitialized' ) {
  32. $(this).attr('src', 'missing.jpg');
  33. }
  34. }); })
Add Comment
Please, Sign In to add comment