0

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?

user2966197
  • 2,793
  • 10
  • 45
  • 77
  • 1
    `() => {` is ES2015 syntax, that Internet Exploder does not understand - therefore, your javascript can not be *parsed* in Internet Exploder - therefore *none of it can be executed* - if you need to use *modern* javascript and still support internet exploder, look into transpilers (such as babeljs) TL;DR - since the code is not *parsable* by IE, none of it runs - nothing at all to do with Internet Exploders lack of AudioContext – Jaromanda X May 25 '18 at 06:09
  • @JaromandaX I am not too familiar with javascript(very very basic understanding) but since I am doing check for `audioContext` value before that line so shouldn't that line be not executed at all as I am assuming in IE my `audioContext` value should be equal to `undefined`. – user2966197 May 25 '18 at 06:15
  • Code needs to parse, that is, be syntactically correct. IE doesn't understand `=>` which means the whole "block" of javascript (script tag?) is discarded. Think of it as "compiling" - you can't execute a program that doesn't "compile" – Jaromanda X May 25 '18 at 06:16
  • @JaromandaX ok. So how can I modify my code so that it works in IE as well as other browsers. I am using `audioContext.resume().then(() => {` because chrome browser needs to resume audioContext as it by default pauses the audioContext until a user action happens – user2966197 May 25 '18 at 06:27
  • don't use ES2015 syntax at all ... so `.then(function() {....})` instead of `.then(() => {....})` - but also read about the difference between *regular* functions and [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) - the code you've shown seems to be unaffected by using regular instead of arrow -when you wrote this code, why did you decide to use an arrow function there? – Jaromanda X May 25 '18 at 06:32
  • just a follow up, imagine you had python code (I see you are active in Python tag) `if False: blah blah blah` - exactly that, as written - now, False is never True, but you still get a `SyntaxError: invalid syntax` error, and the code doesn't run :p – Jaromanda X May 25 '18 at 06:36
  • @JaromandaX I used the arrow function as I saw example of audioContext.resume using it so I though that was the way to use it(like I said my lack of knowledge in javascript :p). Now I got it(with python example)! – user2966197 May 25 '18 at 06:48
  • that's the problem with examples on the internet these days - they all assume you either don't want to support IE, or know how to support IE if you have to :p – Jaromanda X May 25 '18 at 06:49

0 Answers0