1

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>
2
  • Well... if you're importing a module, you're loading a file. You can write code within the <script> tag though Commented Mar 13, 2020 at 17:08
  • You are right, I updated my question. Commented Mar 13, 2020 at 17:11

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.