I have this JS
code where I am checking if AudioContext
is defined then set it otherwise return undefined
:
function audioContextCheck() {
if (typeof AudioContext !== "undefined") {
return new AudioContext();
} else if (typeof webkitAudioContext !== "undefined") {
return new webkitAudioContext();
} else if (typeof mozAudioContext !== "undefined") {
return new mozAudioContext();
} else {
// return undefined
return "undefined"
}
}
var audioContext = audioContextCheck();
console.log(audioContext)
function audioRecording( e ) {
try{
console.log('audioContext is '+ audioContext)
if (audioContext != "undefined") {
audioContext.resume().then(() => {
console.log('Resumed audioContext on end-recording');
e.classList.remove('recording');
recording = false;
});
}
else {
console.log('IE audioContext not supported')
e.classList.remove('recording');
recording = false;
socketio.emit('end-recording');
}
}
catch(error){
console.log(error)
console.log('Resumed audioContext on end-recording');
e.classList.remove('recording');
recording = false;
}
}
}
Now this JS
file is loaded from a html
page. But in Internet Explorer 11
whenever I load this page I can see syntax error
at line audioContext.resume().then(() => {
. I know IE does not support AudioContext
and that's why I am using audioContextCheck()
where I return undefined
if AudioContext
is not supported and then in audioRecording()
I check if varaible audioContext
is not equal to undefined
then only execute the code under it. So for IE
it should not execute that code but it still throws syntax error at the line.
Also I have console.log()
statement in my code but I do not see anything coming on the console in IE 11
(in dev tools).
What is the mistake I am doing here and what would be a more elegant way to make sure IE does not go to the error throwing line and thus does not throw error?