admin管理员组

文章数量:1287080

I'm using a long HTML Template script in JS file, like:

var TEMPLATE = `
<div>
  <ul>
    <li>first</li>
    <li>second</li>
    <li>third</li>
  </ul>
</div>`;

It works in all browsers(including Chrome, Safari, Firefox & EDGE) but not in Internet Explorer 11, 10.

Can you suggest how I can fix this?

I'm using a long HTML Template script in JS file, like:

var TEMPLATE = `
<div>
  <ul>
    <li>first</li>
    <li>second</li>
    <li>third</li>
  </ul>
</div>`;

It works in all browsers(including Chrome, Safari, Firefox & EDGE) but not in Internet Explorer 11, 10.

Can you suggest how I can fix this?

Share Improve this question edited Feb 25, 2019 at 12:39 Robbie JW 7391 gold badge9 silver badges22 bronze badges asked Oct 2, 2018 at 10:32 om_jaipurom_jaipur 2,1965 gold badges31 silver badges54 bronze badges 6
  • 4 Try using Babel: babeljs.io/docs/en – Michelangelo Commented Oct 2, 2018 at 10:34
  • 4 It's because you're using template literals which aren't supported in IE, you can change it to a normal string or as @Michelangelo has said, use Babel – George Commented Oct 2, 2018 at 10:35
  • Yes, I got it that "template literals aren't supported in IE". @Michelangelo, Can you describe how can I use Babel? I'm using AngularJS. – om_jaipur Commented Oct 2, 2018 at 10:51
  • 1 Possible duplicate of Template literals not working in IE11 when "use strict" directive is used – Stuart Commented Oct 2, 2018 at 11:04
  • 1 @om. No, this is not a programming class and I am not a teacher. The docs are clear. You have to figure this one out on your own. – Michelangelo Commented Oct 2, 2018 at 11:13
 |  Show 1 more ment

1 Answer 1

Reset to default 13

If you don't need any of the advanced features available in template literals (such as ${foo}), you might also consider using regular quotes and just escape the new lines to prevent syntax errors, like so:

var list = '\
    <div>\
        <ul>\
            <li>first</li>\
            <li>second</li>\
            <li>third</li>\
        </ul>\
   </div>\
';

本文标签: javascriptES6Template strings hack for IE 11Stack Overflow