Navigate back to the homepage

how to call script type module function in javascript?

Infinitbility
JavaScript
July 26th, 2022 · 1 min read
how to call script type module function in javascript?

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

  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…

1<!DOCTYPE html>
2<html>
3 <head>
4 <title>Infinitbility</title>
5 <meta charset="UTF-8" />
6 </head>
7
8 <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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to upload base64 image to ipfs?

how to upload base64 image to ipfs?

An example of uploading base64 image to ipfs in javascript with the step-by-step guide.

July 26th, 2022 · 1 min read
how to get browser name in react js?

how to get browser name in react js?

An example of get browser name in react js with step by step guide.

July 25th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]