-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?

6
  • 2
    your firmlogo is a reference to a div object, not an actual URL Commented May 6, 2015 at 19:43
  • Do you have a server route setup to handle that get request? Commented May 6, 2015 at 19:44
  • 1
    why dont you use alt ? Commented May 6, 2015 at 19:48
  • What is firmlogo is this a url path to an image on the same domain as the get? Commented May 6, 2015 at 19:49
  • Yes, exactly @Huangism Commented May 6, 2015 at 19:58

2 Answers 2

4

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

<h2><img src="bad_address" alt="Display this text instead" /></h2>

Sign up to request clarification or add additional context in comments.

1 Comment

The only problem with this is that you get the image box around the alt text
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");}

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.