JavaScript in IE11 giving me script error 1003

时间秒杀一切 提交于 2019-12-10 14:00:55

问题


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.


回答1:


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 },


来源:https://stackoverflow.com/questions/30853903/javascript-in-ie11-giving-me-script-error-1003

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!