0

I wish to test from javascript within my html page if the following files exist or not:

http://www.focloir.ie/media/ei/sounds/ag_c.mp3  // exists
http://www.focloir.ie/media/ei/sounds/og_c.mp3  // doesn't exist

My html page is not on the same domain as the files above.

Is this possible?

I wish to solve this using only a single html file (no db or php etc.) since my project is just a simple prototype.

9
  • 2
    Yes it's possible, you write a serverside script that checks if the file exists, and call that with ajax from your own server. Javascript has no access to external .mp3 files, as it has a same origin policy. Commented May 14, 2013 at 9:39
  • @adeneo OK so this is absolutely not solvable with just a single html file. In other words, I can't write a simple jsfiddle to do this lookup. Commented May 14, 2013 at 9:42
  • Not really, but you could always use YQL, or link to the file directly and see if it loads ? Commented May 14, 2013 at 9:44
  • JSON Commented May 14, 2013 at 9:45
  • @adeneo What do you mean by "link to the file"? Commented May 14, 2013 at 9:48

1 Answer 1

1

This looks promising, although super slow and ugly :)

var _word = "ag";
var _audio = Audio();

function update_src(audio, word) {
    if (audio.canPlayType('audio/mpeg;')) {
        audio.src = "http://www.focloir.ie/media/ei/sounds/" + word + "_c.mp3";
    } else {
        audio.src = "http://www.focloir.ie/media/ei/sounds_ogg/" + word + "_c.ogg";
    }
}

update_src(_audio, _word);
_audio.load();
_audio.addEventListener("durationchange", function () {
    alert("exits");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.