run script before page load

Solutions on MaxInterview for run script before page load by the best coders in the world

showing results for - "run script before page load"
Fabien
24 Apr 2018
1//Use the defer attribute on a classic script tag:
2
3<script defer src="./my-code.js"></script>
4
5//Use JavaScript modules. A type="module" script is deferred until the 
6//HTML has been fully parsed and the initial DOM created.
7
8<script type="module" src="./my-code.js"></script>
9<!-- Or -->
10<script type="module">
11// Your code here
12</script>
13
14//Add the script to the bottom of the <body> right before the </body>.
15
16<!doctype html>
17<html>
18<!-- ... -->
19<body>
20<!-- The document's HTML goes here -->
21<script type="module" src="./my-code.js"></script><!-- Or inline script -->
22</body>
23</html>