1
I’m trying to make a dynamic file call where I have one input
that receives a name and is loaded a file with this name in the <source>
in js, when the person enters a file name that does not exist in the folder would need to display an error message and not execute the play()
, but for some reason in the code below the catch
doesn’t catch the error. I thought of doing a check if the file exists before assigning the name, but I would like a simple solution without needing API or anything like that and did not find.
In the end I need a way or treat the mistake GET net::ERR_FILE_NOT_FOUND
or to check in a simple way, without needing API, whether the file exists before assigning (if possible, of course).
Follows 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++);
}
I believe the best way is to check if the file exists, because the Trycatch which is doing is not giving error in the execution of the code but rather file error not found, which would not fall in the catch.
– Douglas Garrido