Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript in IE11 giving me script error 1003

I have a site with an accordion and some javascript. In Firefox everything is working as it should, but in IE11 I get the error

SCRIPT1003: Expected ':'

I narrowed it down to this piece of code in my .js file:

var nmArray = new Array();

function saveplayers() {
  var x;

  for (x=0;x<32;x++) {
    var y = "i"+eval(x+1);
    nmArray[x]=document.getElementById(y).value;
  }
  var request = $.ajax({
    type: "POST",
    url: "savep.php",
    data: ({ nmArray }),
    cache: false
  });
}

The error complains there should be a colon after nmArray in ({ nmAray })

If I take this function out, my site works again. For debugging I stripped down my HTML, and I'm not even calling this function. I just included the .js file.

like image 243
notaverygoodprogrammer Avatar asked Sep 27 '22 14:09

notaverygoodprogrammer


1 Answers

The syntax ({nmArray}) in a browser that supports ES6 is a shortcut for {nmArray: nmArray}. IE11 doesn't support this feature (based on the error you're receiving), so you'll have to rewrite it as:

data: ({ nmArray: nmArray }),

See here for an example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_6

note that in this case you can omit the wrapping ()

data: { nmArray: nmArray },
like image 65
Kevin B Avatar answered Oct 05 '22 07:10

Kevin B