1

I'm making a search tool where all employees of a company can find their names. these values ​​are retrieved using json. (this includes a link to a image of the employee).

My question. Sometimes the link doesn't exists (wrong input from the other system due to automatic generation). If thats so i want to replace the image with a default one. How can i check if a image-link doesn't exists/is wrong?

    for ( i = 0; i < totalResultsToShow; i++) {
        //when the photoURL isn't given     
        if (searchResults[i].photoURL == "") {
            searchResults[i].photoURL = imagesURL + "silhouette.jpg";
        }
        //when the photoURL doesn't exists

        //do something??

        HTMLContent += "<div class=resultTableRow id=" + i + " onclick=resultClicked(" + i + ");>";
        HTMLContent += "<div class=resultTableItemImage><img width ='" + resultsImageWidth + "' src='" + searchResults[i].photoURL + "' class=stretchWidth /></div>";
        HTMLContent += "<div class=resultTableItemUser><p>" + searchResults[i].user + "</p></div>"
        HTMLContent += "<div class=resultTableItemUsername><p>" + searchResults[i].username + "</p></div>"
        HTMLContent += "</div>"; "</div>";
    }
//put results on screen

I think 1 solution is to put the code for the image in a try/catch block. But i personally dont like try/catch blocks.

[EDIT 1] I agree with you about checking for existence and undefined. Only the link does exists in the Results array. But its a corrupt link. Is there any way to check links before adding them to the html in Javascript?

[EDIT 2] The error im getting is: GET http://...... /---.jpg 404 (Not Found)

[SOLUTION] notice the onerror=this.src='"+imagesURL+"/silhouette.jpg'

HTMLContent += "<div class=resultTableItemImage><img width ='" + resultsImageWidth + "' src='" + searchResults[i].photoURL + "' class=stretchWidth onerror=this.src='"+imagesURL+"/silhouette.jpg' /></div>"; 

Thnx

Marcel
  • 1,494
  • 1
  • 23
  • 33
  • 1
    I think this answers your question: http://stackoverflow.com/questions/9022427/see-if-src-of-img-exists – Lian Sep 18 '12 at 07:03
  • something like this indeed. Is it possible to correct the img src directly in the onerror? – Marcel Sep 18 '12 at 07:20

4 Answers4

1
if (typeof searchResults[i].photoURL === "undefined") {
    searchResults[i].photoURL = imagesURL + "silhouette.jpg";
}
chovy
  • 72,281
  • 52
  • 227
  • 295
1
if (!searchResults[i].photoURL) {
        searchResults[i].photoURL = imagesURL + "silhouette.jpg";
    }
secondflying
  • 871
  • 1
  • 7
  • 16
1

You can check for the existence of searchResults[i].photoURLby saying:

if(searchResults[i].photoURL) { 
    // your code 
} else { 
    // what to do if not found
}
Eivind Eidheim Elseth
  • 2,294
  • 1
  • 23
  • 28
0

[SOLUTION] notice the onerror=this.src='"+imagesURL+"/silhouette.jpg'
Html + javascript in 1 line.

HTMLContent += "<div class=resultTableItemImage><img width ='" + resultsImageWidth + "' src='" + searchResults[i].photoURL + "' class=stretchWidth onerror=this.src='"+imagesURL+"/silhouette.jpg' /></div>"; 

when an error occurs the 'onerror=' replaces the given src with the provided src behind the onerror

Marcel
  • 1,494
  • 1
  • 23
  • 33