Javascript IE11 抛出“SCRIPT1014:无效字符”,所有其他浏览器都可以使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43048455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE11 throwing "SCRIPT1014: invalid character" where all other browsers work
提问by Damien
Here's a new one, IE11 is throwing errors on code that works in every other browser.
这是一个新问题,IE11 会在适用于所有其他浏览器的代码上抛出错误。
Anyway, after a few hours of "fixing" code so that IE11 doesn't fall on its own face I have come across an error I just cannot seem to find a solution to. Here is the code in question:
无论如何,经过几个小时的“修复”代码,使 IE11 不会出现问题,我遇到了一个错误,我似乎无法找到解决方案。这是有问题的代码:
$('input[name="messageAccount"]').change(function () {
$aButton.show();
var addedIds = $("#hdnfield").val();
if (addedIds == null || addedIds === "") {
$("#hdnfield").val(this.value);
} else {
$("#hdnfield").val(`${addedIds}${this.value}`);
};
});
This is nested on the inside of the success call in an Ajax request. IE's debugger is saying the error is coming from the content of the elsestatement but it's also reading all the brackets wrong. For example the opening parenthesis on this function is being "closed" after the closing bracket for the Ajax request... (hopefully that made sense).
这嵌套在 Ajax 请求中的成功调用内部。IE 的调试器说错误来自else语句的内容,但它也读取了所有括号错误。例如,此函数的左括号在 Ajax 请求的右括号之后被“关闭”......(希望这是有道理的)。
Has anyone else had a similar issue with IE before? I've got a number of other bugs to fix so if there are any replies I will post back as soon as I can. Thanks in advance.
之前有没有其他人遇到过类似的 IE 问题?我还有许多其他错误需要修复,因此如果有任何回复,我会尽快回复。提前致谢。
EDIT: just for reference I am currently running this locally and is part of what will become an internally hosted web app.
编辑:仅供参考,我目前正在本地运行它,并且是将成为内部托管网络应用程序的一部分。
回答by Tom
It's because "`" (template strings) is not supported by IE : http://caniuse.com/#search=string
这是因为 IE 不支持“`”(模板字符串):http: //caniuse.com/#search=string
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
See https://stackoverflow.com/a/27678299/2195875
见https://stackoverflow.com/a/27678299/2195875
To avoid this error you can either use a ES6 to ES5 transpiler, or just directly write valid ES5 javascript:
为了避免这个错误,你可以使用 ES6 到 ES5 的转译器,或者直接编写有效的 ES5 javascript:
instead of
代替
`string text ${expression} string text`
`字符串文本 ${表达式} 字符串文本`
write
写
"string text "+expression+" string text"
"字符串文本"+表达式+"字符串文本"
回答by goksel
You can also add polyfill to your html:
您还可以将 polyfill 添加到您的 html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.min.js"/>

