.js
extension
// myfile.js
function sayHello() {
alert("Hello, JavaScript!");
}
// Call sayHello() as soon as DOM finishes loading
addEventListener("DOMContentLoaded", sayHello);
.js
file in <script>
tag
<head>
<script src="myscript.js"></script>
</head>
<script>
tag and not place
any JavaScript inside it
<script>
tags to import multiple scripts
<script>
tag causes the browser to halt parsing, load the script file,
run the code, then resume parsing
(see visual)
defer
- Don't run the script until the page has loaded
<script src="myscript.js" defer></script>
document.writeln()
statements
async
- Run the script asynchronously as soon as it is available
<script src="myscript.js" async></script>
document.writeln()
statements
or modify the DOM immediately