title: How to replace a broken img url with a default when the broken url does not exists
body: <p>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).</p>
  
  <p>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?</p>
  
  <pre><code>    for ( i = 0; i &lt; 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 += "&lt;div class=resultTableRow id=" + i + " onclick=resultClicked(" + i + ");&gt;";
          HTMLContent += "&lt;div class=resultTableItemImage&gt;&lt;img width ='" + resultsImageWidth + "' src='" + searchResults[i].photoURL + "' class=stretchWidth /&gt;&lt;/div&gt;";
          HTMLContent += "&lt;div class=resultTableItemUser&gt;&lt;p&gt;" + searchResults[i].user + "&lt;/p&gt;&lt;/div&gt;"
          HTMLContent += "&lt;div class=resultTableItemUsername&gt;&lt;p&gt;" + searchResults[i].username + "&lt;/p&gt;&lt;/div&gt;"
          HTMLContent += "&lt;/div&gt;"; "&lt;/div&gt;";
      }
  //put results on screen
  </code></pre>
  
  <p>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.</p>
  
  <p>[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?</p>
  
  <p>[EDIT 2]
  The error im getting is: GET http://...... /---.jpg 404 (Not Found)</p>
  
  <p>[SOLUTION] notice the onerror=this.src='"+imagesURL+"/silhouette.jpg'</p>
  
  <pre><code>HTMLContent += "&lt;div class=resultTableItemImage&gt;&lt;img width ='" + resultsImageWidth + "' src='" + searchResults[i].photoURL + "' class=stretchWidth onerror=this.src='"+imagesURL+"/silhouette.jpg' /&gt;&lt;/div&gt;"; 
  </code></pre>
  
  <p>Thnx</p>