script
Loads or contains JavaScript.
Learn HTML Scripts with practical examples for structure, layout semantics, metadata, embedded content, scripts, accessibility, SEO and live practice.
HTML structure and layout
HTML can load JavaScript with the script element. Scripts can add interactivity, fetch data, validate forms, update the DOM and power full applications.
Script loading is a performance decision. A blocking script in the wrong place can delay rendering. Modern pages commonly use defer for normal page scripts.
Good script markup is deliberate: source, type, defer or async, placement and security all matter.
Loads or contains JavaScript.
Points to an external JavaScript file.
Runs after HTML parsing, preserving order.
Runs when downloaded, useful for independent scripts.
Syntax and structure
Use external files for maintainable code. Use defer for normal scripts that enhance the page after parsing.
<script defer src="/assets/js/app.js"></script> <script type="module" src="/assets/js/course.js"></script>
<button onclick="alert('Hi')">Click</button>
<script src="/heavy-app.js"></script>
<script>document.write("Loading...")</script>
Rules that matter
Layout-focused HTML is strongest when meaning, grouping, metadata and behavior hooks all have a clear reason to exist.
JavaScript belongs in reusable files for real projects.
Deferred scripts wait until the HTML is parsed and preserve order.
Analytics or independent widgets can use async when order does not matter.
addEventListener keeps behavior cleaner than onclick attributes.
External vendors can affect performance, privacy and security.
Progressive enhancement makes pages more resilient.
Production thinking
This matters because JavaScript can make a page feel alive, but careless script loading can also make it slow, fragile and hard to maintain.
JavaScript should not remove keyboard access, focus order or native element behavior. Start with correct HTML controls.
Bundle size, loading strategy, error monitoring and Content Security Policy matter once scripts become part of a real product.
Important content should not depend entirely on client-side JavaScript if search visibility matters.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.