JavaScript in HTML means connecting behavior to your markup with the <script> element. HTML builds structure, CSS styles it, and JavaScript responds to clicks, updates text, and validates forms—without reloading the page.
What does the HTML script element do?
It tells the browser to run JavaScript as part of the document. Code can live inside the tag (inline) or in a separate .js file linked with src.
When should you add scripts to HTML?
Whenever the page needs interactivity: buttons, live text updates, form checks, or talking to APIs. Start with a small inline <script> at the end of <body>, then move logic into external files as the project grows.
HTML vs CSS vs JavaScript
Keep roles clear in your HTML project: markup for content, CSS for presentation, and scripts for behavior. Mixing large scripts into every page makes HTML hard to maintain.
Baseline · Widely available.
Every modern browser includes a JavaScript engine. You do not install plugins to run the HTML examples in this tutorial.
Order in the HTML document matters.
A script that calls getElementById before that element appears in the HTML will fail. Place the script after the markup, or use defer on external files in <head>.
Prefer external .js for real HTML sites.
Inline scripts are great for learning. Reusable behavior belongs in files linked with <script src="app.js"></script>.
Syntax
HTML Syntax
syntax.html
<script>/* JavaScript */</script>
external.html
<script src="app.js" defer></script>
Both start and end tags are required. With src, leave the element empty—do not put code between the tags. See the full <script> tag reference for async, type="module", and more.
Reference
Core HTML Concepts
Terms you will use when wiring JavaScript into an HTML document:
HTML first. The paragraph with id="status" is in the document before the script.
Script runs when the browser reaches the <script> tag while parsing.
getElementById finds that HTML element in the DOM.
textContent replaces the visible text without reloading the page.
Example 2 — Script at the End of <body>
Place the script after your HTML content so elements exist before the code runs.
HTML
<body>
<h1>Page Content Loads First</h1>
<p id="note">This paragraph exists before the script runs.</p>
<script>
document.getElementById('note').textContent =
'Script ran after the HTML was parsed.';
</script>
</body>
console.log('This message is from an external JavaScript file.');
var log = document.getElementById('log');
if (log) {
log.textContent = 'This message is from an external JavaScript file.';
}
HTML holds content and a button that names the function to call.
complete.js defines changeText outside the HTML file.
defer ensures the script runs after the HTML is ready.
Separation of concerns: markup vs behavior—easier to reuse and test.
Edge Cases & Pitfalls
Most HTML + JavaScript bugs are placement, wrong paths, or mixing code into src scripts.
null
Script runs before the HTML element exists
getElementById returns null. Move the script below the markup or use defer.
src
Code between tags ignored when src is set
With <script src="...">, leave the element empty. Inline code next to src is not executed.
path
Broken script src path
Check the Network tab. The HTML file’s folder must resolve app.js correctly.
inline
Huge scripts inside every HTML page
Move shared logic to external files so HTML stays readable and cacheable.
valid
Trusting only client-side form checks
HTML + JS validation improves UX, but always validate again on the server.
csp
Inline handlers blocked by CSP
Strict Content-Security-Policy setups may block onclick. Prefer external scripts and addEventListener.
Compatibility
Browser Support
The HTML <script> element, src, defer, and core DOM APIs like getElementById are supported in every modern browser.
✓ Baseline · Since HTML4
script in HTML
All browsers ship a JavaScript engine. The HTML techniques in this tutorial work in Chrome, Firefox, Safari, Edge, and mobile browsers without plugins.
100%Core script support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
<script> & DOM APIsUniversal
Bottom line: Write standard HTML and JavaScript—it runs everywhere your users browse.
Remember
Key Takeaways
HTML hook: add behavior with <script> (inline or src).
Placement: end of <body>, or defer in <head>.
External files keep HTML clean and reusable across pages.
DOM + events: update HTML live; use onclick for demos, listeners for apps.
One line: HTML structure first → <script> after content (or defer) → change the DOM / handle events.
Frequently Asked Questions
Use the HTML script element. Put code between <script> and </script> for inline JavaScript, or link a .js file with <script src="app.js"></script>. The browser downloads and runs it as part of the HTML document.
For beginners, place scripts at the end of body so HTML elements exist before the script runs. Scripts in head run early and may fail if they query elements that are not parsed yet. A modern pattern is <script src="app.js" defer> in head.
Inline JavaScript lives inside the HTML file in a script block. External JavaScript is a separate .js file linked with the src attribute on script. External files keep HTML cleaner and can be reused across pages.
The Document Object Model is the browser’s live tree of your HTML. JavaScript can read and change it—update text, toggle classes, show or hide elements—without reloading the page.
onclick is fine for learning and tiny demos. Production HTML usually keeps markup clean and attaches listeners with addEventListener in a script block or external file.
Not for basic pages—open the .html file in a browser. Some features (modules, fetching local files) need a local server, but the examples in this tutorial work from a normal HTML file.