I'm trying to make a dynamic file call where I have a input
that gets a name and a file with that name is loaded in the <source>
in js, when the person types a file name that does not exist in the folder you would need to display an error message and not run play()
, but for some reason in the code below the catch
does not catch the error. I thought of doing a check if the file exists before assigning the name, but I'd like a simple solution without needing API or anything like that and have not found it.
In the end I need a way or to handle the error GET net::ERR_FILE_NOT_FOUND
or to verify in a simple way, without needing API, if the file exists before assigning (if possible, of course).
Follow the Code:
function play (element) {
try {
audioElement[played] = document.createElement('audio');
audioElement[played].innerHTML = '<source src="../_songs/' + element.parentNode.getElementsByClassName("musica-location")[0].value + '.mp3"'+ ' type="audio/mpeg" />';
audioElement[played].play();
} catch (err) {
alert("Erro, arquivo não encontrado", err);
}
element.setAttribute("data-audio", played++);
}