Search code examples
javascriptbabeljsbabel-polyfill

JavaScript ES6 in Internet Explorer 11


I am trying to run a Javascript code in Internet Explorer 11.

It not working with arrow functions or ``, however I need these sintax because I would like to work more comfortable and do not have some issues with this.

I trying with Babel at top and in the end of the page, but I always have the same error in IE11:

Character not valid

This error is because ${}

It is the library that I am using:

<script src='https://unpkg.com/[email protected]/babel.js'></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.10.4/polyfill.min.js" integrity="sha512-pgx+g4+yrNG/DhUEosN7ontsnk2WayfSuK1Fmy2GrqNi8ktUpIlTpKj80dCLyoEELA6c6m5otWP9RM/lq5UTQg==" crossorigin="anonymous"></script>

What I am doing bad?


Solution

  • First of all, using babel like this should be used mostly for prototyping. As official babel setup guide says:

    Compiling in the browser has a fairly limited use case, so if you are working on a production site you should be precompiling your scripts server-side. [...]

    If you still want to use babel in current way, remember that you need to mark your script tag as text/babel.

    <script type="text/babel">
        // Your script content
    </script>
    

    I would reccomend using some kind of build system like Webpack which configured correctly will execute babel scripts for you.

    You can use Babel as a CLI or require hook too, but for that I am sending you to the official babel setup documentation.