Cyber Code Academy
Tutorials Academy Courses
Home / html-tutorials-md / HTML Script and NoScript

HTML Script and NoScript

> Load JavaScript in an HTML document, understand defer and async, and provide useful fallback content.

**Cyber Code Academy · HTML Tutorial · Lesson 30 · Intermediate**

## 1. What is the script element?
The script element is used to include JavaScript in an HTML document. JavaScript can respond to user actions, update the page, validate data, request server resources, and provide interactive behaviour.

HTML should describe the content and structure. JavaScript adds behaviour.

## 2. Inline JavaScript

“`html

console.log(“Welcome to HTML learning!”);

“`

The browser processes the script as part of the HTML document. For maintainability and security, larger scripts are generally better kept in separate files.

## 3. External JavaScript

Project structure:
“`text
website/
├── index.html
└── js/
└── app.js
“`

HTML:
“`html

“`

JavaScript file:
“`javascript
console.log(“External JavaScript loaded.”);
“`

The src attribute tells the browser where the script resource is located. Check the Network panel if the file fails to load.

## 4. The defer attribute

“`html

“`

For a classic external script, defer allows downloading while HTML parsing continues. The script executes after the document has been parsed and before DOMContentLoaded. Deferred classic scripts preserve their document order.

This is often a good default for scripts that need to work with the page’s HTML.

## 5. The async attribute

“`html

“`

async allows a classic external script to execute as soon as it is available. Its execution order relative to other async scripts is not guaranteed.

Use async for independent scripts that do not depend on another script’s execution order.

## 6. Difference between async and defer

| defer | async |
|—|—|
| Executes after parsing | Executes when download completes |
| Preserves order among deferred classic scripts | Order is not guaranteed |
| Suitable for scripts that depend on parsed DOM | Suitable for independent scripts |

Do not add both attributes without understanding how the script should execute.

## 7. Module scripts

“`html

“`

JavaScript modules have their own loading and execution rules. They are deferred by default and support import/export. Modules also run in strict mode.

## 8. The noscript element

noscript provides fallback content when scripting is disabled or unsupported in the relevant browsing context:

“`html

This website needs JavaScript for some interactive features.

“`

Do not put all essential page content behind JavaScript unless the experience genuinely requires it. Core informational content should remain available where practical.

## 9. Avoid inline event-handler attributes

Avoid patterns such as:
“`html

“`

A more maintainable approach:
“`html

“`

“`javascript
document.querySelector(“#hello-button”).addEventListener(“click”, () => {
alert(“Hello”);
});
“`

This keeps behaviour separate from markup and works better with a restrictive Content Security Policy.

## 10. Common mistakes

– Incorrect script file path.
– Running DOM-dependent code before the elements exist.
– Using async for scripts that depend on execution order.
– Confusing a script download with a completed script execution.
– Putting secrets in client-side JavaScript.
– Assuming noscript is a security feature.

## 11. Practice exercises

1. Create a separate app.js file.
2. Load it using defer.
3. Add a button and handle its click using addEventListener.
4. Load an independent script using async and explain when it executes.
5. Add a noscript message.
6. Inspect the Network and Console panels.

## 12. Knowledge check

1. What does script do?
2. How do inline and external scripts differ?
3. What does defer do?
4. Why is async execution order unpredictable?
5. What is a JavaScript module?
6. When does noscript content appear?
7. Why are event listener functions often preferable to onclick attributes?

**Next lesson:** HTML, CSS, and JavaScript Integration.

Want to learn this professionally?

Explore practical IT training at Cyber Code Academy.

View Academy Courses