Fifi
Fifi

Reputation: 3605

In JavaScript, can we import a JS module in the <script></script> tag without loading an external file?

Client side, can we import a JavaScript module in the <script></script> tag of a HTML page without loading an external script?

<html>
  <head>
  ...
  </head>
  <body>
  ...
    <script>
      document.getElementById('btn').onclick = () => { 
        // Import a JS module here
      }
    </script>
  </body>
</html>

Upvotes: 1

Views: 46

Answers (1)

Mr Robot
Mr Robot

Reputation: 1216

Do not use import * as label from "file.js". Use the following code:

<html>
  <head>
  ...
  </head>
  <body>
  ...
    <script>
      document.getElementById('btn').onclick = () => { 
        // Import a JS module here
        import("file.js").then(obj => {
          // Do something with obj
        })
      }
    </script>
  </body>
</html>

Upvotes: 1

Related Questions