-2

In my javascript code, I'm using constants and while if works correctly in Chrome and Firefox, I get an undefined error in IE11. Emulation mode is set to agent string = Microsoft Edge. I read that in IE v5 - 9 (and 10 preview) constants are not available, but from 11 on it should be ok. What am I doing wrong and how to solve it? Check on browser type and if IE then just make variables for it? So I have a x.js file with this code:

const CONST_FAIL_1 = "something failed";
const CONST_FAIL_2 = "something else failed";

then, in my code I include the x.js file

<script language="Javascript" src="/scripts/x.js"></script>

and further down the page I have this javascript function:

function doSomething(x){
    try{
        if (ajaxCreateObjectinDB(x)){
            return true;
        }
        else{
            return false;
        }
    }
    catch(err){
    showErrorDiv(true, CONST_FAIL_1 + ":" + err.message);
        return false;
    }
}

I get the SCRIPT5009: 'CONST_FAIL_1' is undefined javascript error

Also, default for a function variable is not recognized. So:

function x(x,y=true){....}

gives me an error that the ')' is missing after 'y'.

Any input appreciated.

user9671207
  • 33
  • 2
  • 8
  • What is "an undefined error"? IE 11 supports `const` except in for..in and for..of loops so you'll have to show your code. There's nothing you can do about default parameters other than run the code through Babel or similar. – Guy Incognito May 04 '20 at 15:39
  • Does this answer your question? [Javascript Functions and default parameters, not working in IE and Chrome](https://stackoverflow.com/questions/15178700/javascript-functions-and-default-parameters-not-working-in-ie-and-chrome) – Joshua Rose May 04 '20 at 15:47
  • @Guy Incognito: I edited my question – user9671207 May 04 '20 at 15:52
  • @Joshua Rose: yes, it confirms my thoughts as how to solve it. Thank you – user9671207 May 04 '20 at 15:53

1 Answers1

1

As we all known, the Const declaration support IE 11 browser, instead of IE 5~9. If you want to declare a variable, you could try to use the var statement. Code as below:

var CONST_FAIL_1 = "something failed";
var CONST_FAIL_2 = "something else failed";

Since, the Default function parameters don't support IE browser, if you want to set the default value, you could check it in the function method, like this:

    function test(x, y) {
        if (y == undefined) {
            y = true;  // set the default value.
        }
    };
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Yes, but the thing is that it doesn't work in IE11, as stated. And I don't understand why. I would have to have to convert all the constants to var – user9671207 May 05 '20 at 11:15
  • You could try to use F12 developer tools Network tab to check whether the js file is load success. Perhaps the js file is not load success, so, it will show the "'CONST_FAIL_1' is undefined" error. You could check [this sample code](http://jsfiddle.net/z8kjyw1b/), it works well in IE browser on my side. – Zhi Lv May 07 '20 at 08:31