To link an external JavaScript file in HTML, add a <script> element with a src attribute that points to the file, for example <script src="app.js" defer></script>. The browser downloads the file and runs it as if the code were written inside the page. Keeping code in an external file is the standard approach because it separates structure from behaviour and lets the browser cache the script.
This guide shows the syntax, where to place the tag, how paths work, and how to link scripts that are hosted elsewhere. The topic phrase is external javascript file.
Create two files in the same folder. First index.html:
Then app.js:
Open index.html in a browser. The heading changes and the console shows the message. Note that <script> is written with a closing tag even when it has no content, and a <script> with src must stay empty: any code between the tags is ignored.
| Placement | Behaviour | Recommended |
|---|---|---|
In <head> with defer | Downloads while the page parses, runs after parsing, keeps order | Yes, for most pages |
End of <body> | Runs after the elements above it exist | Works, but starts downloading late |
In <head> without defer | Blocks parsing until the script runs | Avoid |
Scripts that touch the page need the elements to exist. With defer, the script runs after the document is parsed, so getElementById finds the heading. Without it, the same code in the head runs too early and gets null. A related article covers async and defer in detail.
The src value is a URL, resolved against the page address.
If the browser shows a 404 error in the Network tab, the path is wrong. Check capitalisation too, because many servers treat App.js and app.js as different files.
Add type="module" to use import and export inside the file. Module scripts are deferred automatically.
Modules need to be served over HTTP. If you open the page with a file:// address, browsers often block imports, so use a local server such as the Live Server extension.
When you load a script from a content delivery network, you trust that server with your page. Add Subresource Integrity so the browser refuses a changed file:
Replace the hash with the one your provider publishes. Prefer installing libraries with npm and bundling them when possible.
When nothing happens, open the browser developer tools and look in two places. The Console tab shows syntax errors and thrown errors from your script. The Network tab lists app.js with its status: 200 means it loaded, 404 means the path is wrong, and a red entry usually means a blocked or failed request.
Also try adding console.log("app.js loaded") as the first line of the file. If the message appears, the link works and the problem is inside your code. If it does not appear, fix the path, the file name or a cached copy by reloading with Ctrl+Shift+R.
No. The browser downloads the file and ignores the content between the tags.
Check the console for a 404 error or a syntax error, confirm the path and capitalisation, and confirm the script runs after the elements it uses.
Yes. Add one <script> per file. Without async, they run in the order they appear.
No. JavaScript is the default type in HTML, so the attribute is not needed.
Use <script src="path/to/file.js" defer></script> in the head to link an external file, and make sure the path is correct. Use type="module" for import syntax, serve pages over HTTP and add integrity checks for third-party scripts.