-3

I'm trying to create a script that check to see whether a logo exists or not. If it doesn't, I want to use innerHTML to output the firm name instead.

$.get(firmlogo)
    .done(function() { 
        document.getElementById('firmlogo').innerHTML = "<img src="+logooffirm+">";

    }).fail(function() { 
        document.getElementById('firmlogo').innerHTML = "<h2>"+nameoffirm+"</h2>";

    })

It displays the text now, even if the logo is there. The error I get is:

Failed to load resource: the server responded with a status of 404 (Not Found) http://www.example.com/[object%20HTMLDivElement] Failed to load resource: the server responded with a status of 404 (Not Found)

Any ideas on what I'm doing wrong?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2208768
  • 25
  • 1
  • 8

2 Answers2

4

You're reinventing the wheel. HTML has this feature built-in.

<h2><img src="bad_address" alt="Display this text instead" /></h2>
Etheryte
  • 24,589
  • 11
  • 71
  • 116
0

You can use alt to do this without all these stuff. If you want to check if image is available in a certain url use this code.

var image = new Image(); 
image.src = "img.jpg";
if (image.width == 0) {
  console.log("no image");
}
else console.log("image available");

Or you can use the ajax way

$.ajax({
    url: "img.jpg",
    type: "HEAD",
    error: function () { console.log("no image"); },
success:function(){console.log("image available");}

});
tkay
  • 1,716
  • 14
  • 18