EN
HTML - load JavaScript file
3
points
In this article, we would like to show you how to load JavaScript files into HTML.
Quick solution:
<!DOCTYPE html>
<html>
<head>
<script src="/path/to/script.js"></script>
</head>
<body>
Web page content here ...
</body>
</html>
Where: script is loaded before web site is presented.
or:
<!DOCTYPE html>
<html>
<body>
<script src="/path/to/script.js"></script>
</body>
</html>
Where: script is loaded when body is displayed.
or:
<!DOCTYPE html>
<html>
<body>
<script type="module">
import '/path/to/script.js';
</script>
</body>
</html>
Practical example
In this example, we create index.html
file that loads index.js
file.
index.html
file:
<!DOCTYPE html>
<html>
<body>
<script src="index.js"></script>
</body>
</html>
index.js
file:
console.log('The index.js file has been loaded correctly.');
Result: