run js before page load code example

Example: run script before page load

//Use the defer attribute on a classic script tag:

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

//Use JavaScript modules. A type="module" script is deferred until the 
//HTML has been fully parsed and the initial DOM created.

<script type="module" src="./my-code.js"></script>
<!-- Or -->
<script type="module">
// Your code here
</script>

//Add the script to the bottom of the <body> right before the </body>.

<!doctype html>
<html>
<!-- ... -->
<body>
<!-- The document's HTML goes here -->
<script type="module" src="./my-code.js"></script><!-- Or inline script -->
</body>
</html>