To call script type module function in javascript, assign function in window object, then you can call a function from anywhere.

let’s take an example of creating and assigning a function in a window object.

<script type="module">
    window.showAlert = function showAlert() {
        alert("Button Clicked");
    };
</script>

Today, I’m going to show you How do I call script type module function in javascript, as above mentioned here, I’m going to use the window object to make the function accessible.

Let’s start today’s tutorial how do you call script type module function in javascript?

Here we will take the example of

  1. create a function that alert a message
  2. assign function in the window object
  3. create a button and call the function

Let’s write code…

<!DOCTYPE html>
<html>
  <head>
    <title>Infinitbility</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div id="app">
      <button onclick="showAlert()">Show Alert</button>
    </div>
    <script type="module">
      window.showAlert = function showAlert() {
        alert("Button Clicked");
      };
    </script>
  </body>
</html>

In the above code example, we created a function in a script-type module and call it from HTML.

I hope it helps you, All the best 👍.