HTML JavaScript
HTML pages become interactive through JavaScript, and the <script> element is how you connect the two. It can hold code directly or point to an external file. Where you place the script and which attributes you add decide when it downloads and runs, which affects both whether your code can find the page elements and how fast the page appears.
Inline and external scripts
An inline script puts code between the opening and closing tags. An external script uses the src attribute and keeps the tags empty. External files are easier to maintain, can be cached by the browser, and can be shared across many pages.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Greeting demo</title>
<script src="js/app.js" defer></script>
</head>
<body>
<button id="greet">Say hello</button>
<p id="output"></p>
<noscript>Please enable JavaScript to use this page.</noscript>
</body>
</html>
<!-- js/app.js -->
document.getElementById('greet').addEventListener('click', () => {
document.getElementById('output').textContent = 'Hello there!';
});
When the browser runs a script
A plain <script> tag stops the HTML parser. The browser downloads the file, runs it, and only then continues reading the rest of the page. If that script sits in the head and tries to find the button, the button does not exist yet, so getElementById returns null. Two attributes change this behaviour for external scripts:
| Attribute | Download | Runs | Order kept? |
|---|---|---|---|
| none | Blocks parsing | Immediately | Yes |
defer | In parallel | After the HTML is fully parsed | Yes |
async | In parallel | As soon as it arrives | No |
defer is the safe default for your own code. async suits independent scripts such as analytics that do not touch your elements or depend on other files. The older alternative is placing scripts just before </body>, which also works.
JavaScript modules
Adding type="module" lets a script use import and export to split code into files. Module scripts are deferred automatically and run in strict mode. Note that modules usually will not load when you open the HTML file directly from disk, because browsers block module imports from file:// URLs; run a small local server instead.
Scripts can also be added from other servers, such as a library hosted on a content delivery network. When doing so, the integrity attribute lets the browser verify the file has not been altered, and crossorigin="anonymous" is needed alongside it.
The noscript element
Content inside <noscript> appears only when JavaScript is disabled or unavailable. Use it for a short message or a fallback link, not for whole duplicate pages.
Typical errors
The "Cannot read properties of null" error almost always means the script ran before the element existed. Add defer or move the script. Another frequent issue is writing code between the tags of a script that also has src; the browser ignores that inline code entirely. A wrong path in src fails silently on the page, so check the Network tab or Console for a 404.
Inline event attributes such as onclick="..." still work but mix behaviour into markup. Prefer addEventListener in a separate file.
Short version
Keep JavaScript in external files, load them with defer or type="module", and attach behaviour with addEventListener rather than inline attributes.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.