title: Check if sound file exists javascript
body: <p>I have an HTML5 file, with javascript to load sounds. The sound could either be in one of the two locations:</p>
  
  <p><strong>../../Apps/</strong> app name <strong>/sounds</strong>
  <br>or<br>
  <strong>../../Shared/Sounds</strong></p>
  
  <p>What is supposed to happen is that when the function <code>getSound</code> is called, it first checks the first location for the sound, and if the file doesn't exist there, it goes to the second. I've tried a couple of things before but none worked.<p>
  <strong>N.B</strong> Variable <code>blink</code> stores string <strong>"../../Apps/</strong> app name <strong>"</strong></p>
  
  <pre><code>function getSound(s){
     var url = blink + "sounds/" + s;
  
     var testSound = new Audio(url);
     if ( isNan(testSound.duration) ){
         url = "../../Shared/Sounds";
     }
     return new Audio(url);
  };
  </code></pre>
  
  <p>This doesn't work since apparently the duration remains NaN until after it starts playing, so the isNan function always returns true.</p>
  
  <p>I've also tried a <code>fileExists</code> function I found online:</p>
  
  <pre><code>function fileExists(url){
      var http = new XMLHttpRequest();
      http.open('HEAD', url, false);
      http.send();
      return http.status!=404;
  }
  </code></pre>
  
  <p>So all I'm asking is, is there a way to check if a sound file exists using javascript, or if you can check if a sound file was actually loaded to the variable you're using. </p>