Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 - Template strings hack for IE 11

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?

like image 975
om_jaipur Avatar asked Oct 02 '18 10:10

om_jaipur


People also ask

Does IE11 support template literals?

If you look at the ECMAScript 6 compatibility table, you'll see that template literals are not supported by IE11.

What is ES6 template string?

ES6 introduces a new kind of string literal syntax called template strings . They look like ordinary strings, except using the backtick character ` rather than the usual quote marks ' or " . In the simplest case, they really are just strings: context.

Can I use Backticks JavaScript?

Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes. Besides, using backticks makes it easier for string operations.


1 Answers

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>\
';
like image 143
Tell Avatar answered Oct 18 '22 03:10

Tell