Answers for "run script before page load"

1

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>
Posted by: Guest on May-25-2021

Code answers related to "run script before page load"

Code answers related to "Javascript"

Browse Popular Code Answers by Language