How to run JavaScript before the whole page is loaded?

Spread the love

Sometimes, we want to run JavaScript before the whole page is loaded

In this article, we’ll look at how to run JavaScript before the whole page is loaded.

How to run JavaScript before the whole page is loaded?

To run JavaScript before the whole page is loaded, we can load our scripts as modules.

For instance, we write

<script type="module" src="./my-code.js"></script>

or

<script type="module">
  // ...
</script>

to load our script as module from an external script or from inline code.

Conclusion

To run JavaScript before the whole page is loaded, we can load our scripts as modules.

Leave a Reply

Your email address will not be published. Required fields are marked *