I am trying to make a the browser display an alert if an audio element src attribute points to a non existent file, however I do not get any response if I attach the the \"
This should handle both cases (e.g. using with
tags or using
).
See example fiddle.
function handleSourceError(e) { alert('Error loading: '+e.target.src) }
function handleMediaError(e) {
switch (e.target.error.code) {
case e.target.error.MEDIA_ERR_ABORTED:
alert('You aborted the media playback.'); break;
case e.target.error.MEDIA_ERR_NETWORK:
alert('A network error caused the media download to fail.'); break;
case e.target.error.MEDIA_ERR_DECODE:
alert('The media playback was aborted due to a corruption problem or because the media used features your browser did not support.'); break;
case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
alert('The media could not be loaded, either because the server or network failed or because the format is not supported.'); break;
default:
alert('An unknown media error occurred.');
}
}
var toArray = Array.prototype.slice;
toArray.apply(document.getElementsByTagName('audio')).forEach(function(audio){
audio.addEventListener('error', handleMediaError);
toArray.apply(audio.getElementsByTagName('source')).forEach(function(source){
source.addEventListener('error', handleSourceError);
});
});