This example contains two small browser demos:
- A classic-script page that loads two dependent files with defer.
- A module page that imports functions from two other modules.
The module demo should be served from a local development server because browsers load modules using URLs and cross-origin rules.
Create this folder structure:
script-loading-practice/
index.html
main.js
math.js
greeting.js
classic.html
classic-first.js
classic-second.js
Create index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Module script practice</title>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Module script practice</h1>
<p id="module-output">Waiting for the module...</p>
</main>
</body>
</html>The type="module" attribute tells the browser to treat main.js as a module. Module scripts are deferred by default, so the body is parsed before main.js runs.
Create math.js:
export function add(firstNumber, secondNumber) {
return firstNumber + secondNumber;
}The export keyword makes add available to other modules.
Create greeting.js:
export function formatGreeting(personName) {
return "Hello, " + personName + "!";
}This file exports a second function so the entry module can import from more than one dependency.
Create main.js:
import { add } from "./math.js";
import { formatGreeting } from "./greeting.js";
const personName = "Asha";
const sum = add(4, 6);
const greeting = formatGreeting(personName);
const moduleOutput = document.getElementById("module-output");
moduleOutput.textContent = greeting + " The sum is " + sum + ".";
console.info("Module page loaded after HTML parsing.");
console.log("Sum from imported module:", sum);
console.log("Greeting from imported module:", greeting);
console.log(
"Module binding is a window property:",
"personName" in window
);The relative import paths include ./ and the .js file extension. main.js uses the imported functions and updates the page element.
The module binding personName is not added as a property on window. It belongs to main.js module scope.
Create classic.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Classic script practice</title>
<script defer src="./classic-first.js"></script>
<script defer src="./classic-second.js"></script>
</head>
<body>
<main>
<h1>Classic script practice</h1>
<p id="classic-output">Waiting for the scripts...</p>
</main>
</body>
</html>Both scripts use defer. The browser can fetch them while parsing the HTML, then runs them in document order after parsing has finished.
Create classic-first.js:
var classicSharedMessage = "shared global value";
const classicLexicalMessage = "shared lexical value";
console.log("First classic script: loaded");The first classic script declares a top-level var and a top-level const. Later classic scripts can refer to both bindings by name. The var binding also becomes a property of the page's global object.
Create classic-second.js:
console.log("Second classic script can read:", classicSharedMessage);
console.log("Second classic script can read:", classicLexicalMessage);
console.log(
"Top-level var is a window property:",
window.classicSharedMessage === classicSharedMessage
);
document.getElementById("classic-output").textContent =
"Both deferred classic scripts ran in document order.";The second file runs after the first because both external classic scripts use defer and appear in this order.
- Start a local development server in the project folder.
- Open index.html through the server's local HTTP address.
- Open DevTools and select the Console.
- Reload the page if necessary.
- Check the page message and Console output.
The page displays:
Hello, Asha! The sum is 10.
The Console displays messages similar to:
Module page loaded after HTML parsing.
Sum from imported module: 10
Greeting from imported module: Hello, Asha!
Module binding is a window property: false
The final false shows that personName is scoped to main.js instead of being placed on window.
- Open classic.html through the same local server.
- Open the Console.
- Reload the page.
- Check that the first script message appears before the second script messages.
- Read the text written into the page.
The page displays:
Both deferred classic scripts ran in document order.
The Console displays messages similar to:
First classic script: loaded
Second classic script can read: shared global value
Second classic script can read: shared lexical value
Top-level var is a window property: true
In classic.html, temporarily change both defer attributes to async:
<script async src="./classic-first.js"></script>
<script async src="./classic-second.js"></script>Now the browser runs each script as soon as it becomes available. Their order is not guaranteed. Since the second file depends on values from the first, this is not a safe loading choice for these two files.
Restore defer after observing the difference. Use async for scripts that do not depend on another script's execution order.
- In main.js, temporarily change "./math.js" to "./missing.js".
- Reload index.html through the local server.
- Read the Console and Network panel to find the failed module request.
- Restore the correct path and reload again.
A module cannot run if one of its required dependencies cannot be loaded or linked.
- In classic.html, temporarily remove type="module" from the module page's script element, or load main.js from a classic script element.
- Observe the syntax error because static import declarations require module mode.
- Restore type="module".
Keep the two demos separate. The module files use static imports; the classic files share bindings through the page's classic-script environment.
- type="module" tells the browser to load the entry file as a module.
- Module imports and exports make dependencies explicit.
- Module-level bindings do not automatically become properties on window.
- External classic scripts with defer run after parsing and in document order.
- External classic scripts with async have no guaranteed relative order.
- Module imports use relative URLs, so a local development server is useful.
- A missing module path prevents the importing module from running.
| Previous chapter | Chapter lesson | Chapter Q&A | Next chapter |
|---|---|---|---|
| 08 - Identifiers, keywords, reserved words, and naming | Open lesson | Open Q&A | 10 - Variables: const, let, and var |