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.
1<script type="module">2 window.showAlert = function showAlert() {3 alert("Button Clicked");4 };5</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
- create a function that alert a message
- assign function in the window object
- create a button and call the function
Let’s write code…
1<!DOCTYPE html>2<html>3 <head>4 <title>Infinitbility</title>5 <meta charset="UTF-8" />6 </head>78 <body>9 <div id="app">10 <button onclick="showAlert()">Show Alert</button>11 </div>12 <script type="module">13 window.showAlert = function showAlert() {14 alert("Button Clicked");15 };16 </script>17 </body>18</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 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.