Add JavaScript to HTML with inline and external scripts

Add JavaScript to HTML with a <script> element. Write code between its tags or point src at a separate .js file. When code needs page elements, put an inline script at the end of the body or load an external classic script from the head with defer.

Write JavaScript directly in HTML

Save the full example as inline.html in UTF-8 and open it. Click the button and check that the paragraph changes to The text changed..

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Write JavaScript in HTML</title>
</head>
<body>
<h1>Check the script connection</h1>
<p id="message">Click the button.</p>
<button id="change" type="button">Change the text</button>
<script>
const button = document.querySelector('#change');
const message = document.querySelector('#message');
button.addEventListener('click', () => {
  message.textContent = 'The text changed.';
});
</script>
</body>
</html>

The paragraph and button already exist when this script runs. A plain inline script in the head can run before they exist, so querySelector() can return null. Adding defer to an inline classic script does not delay it.

Link a separate JavaScript file

Create index.html and app.js in the same folder. The browser downloads the external file while parsing the HTML and runs it after parsing because of defer. Put JavaScript directly in app.js, without script tags.

index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Link external JavaScript</title>
  <script src="app.js" defer></script>
</head>
<body>
  <h1>Check the script connection</h1>
  <p id="message">Click the button.</p>
  <button id="change" type="button">Change the text</button>
</body>
</html>

app.js

const button = document.querySelector('#change');
const message = document.querySelector('#message');
button.addEventListener('click', () => {
  message.textContent = 'The text changed.';
});

Open index.html and click the button. You should see the same change as in the inline example. If you move the script into a subfolder named js, change the attribute to src="js/app.js".

Choose between defer and async

Method When it runs Suitable for
<script> After the preceding HTML has been parsed Short behavior on one page
<script src="app.js" defer> After parsing and in document order Code that depends on the DOM or other scripts
<script src="app.js" async> As soon as available Independent code

Use defer when the code depends on the DOM, the browser’s representation of the page, or on earlier deferred scripts. If app.js needs settings.js, put settings.js first and give both defer. With async, either can run first. If both attributes appear on an external classic script, async wins. Modules without async already run after parsing. See the HTML Standard on scripts.

Check what failed when a script does not work

  1. Read the Console first for a syntax error or null. For null, compare the selector with the element ID and check when the script runs.
  2. Reload with the Network panel open and look for app.js. For a 404, compare the requested path and spelling, including case, with the real file.
  3. Check the response and its Content-Type even for a 200. If the body is an HTML page, correct the path or server configuration so the URL serves the actual JavaScript.

A relative src uses the document’s base URL, which <base href> can change. Compare the request in the Network panel with the result of this Console expression. Reload after the correction and try the button again. If the Console reports a Content Security Policy restriction, use a script source allowed by that policy.

new URL('app.js', document.baseURI).href

document base URL rules