HTML <script> Element

Publish in HTML Tutorial el 25/05/2025 16:02

The <script> HTML element is used to embed or reference executable code, typically JavaScript, within an HTML document. This powerful element allows you to add interactivity, manipulate the DOM, handle events, and communicate with servers.


Key characteristics of the <script> element:

  • Can contain JavaScript code directly or reference an external file via the src attribute
  • Executes in order of appearance (unless using async or defer)
  • Blocks HTML parsing by default while executing
  • Can be placed in <head> or <body>, though modern practices often recommend before </body>


Basic Syntax

Here's the basic structure of a script element:

Or for external scripts:


Examples

1. Basic Inline Script

This example shows a simple inline script that displays an alert when the page loads:

Play Code


2. External Script

This example demonstrates loading JavaScript from an external file. The external script contains a function that changes the button's text when clicked.

Play Code


3. Script with async Attribute

The async attribute allows the script to download in parallel with HTML parsing and execute as soon as it's available, without blocking.

Play Code


4. DOM Manipulation

This example shows how scripts can manipulate the DOM after the page loads:

Play Code


5. Event Handling

This example demonstrates handling a button click event with JavaScript:

Play Code


Tips and Best Practices

  • Place scripts before </body>: This ensures the DOM is fully loaded before scripts execute.
  • Use external files: For better maintainability and caching benefits.
  • Consider async/defer: Use async for independent scripts, defer for scripts that depend on the DOM.
  • Minify production code: Reduce file size for better performance.
  • Use strict mode: Add 'use strict'; at the beginning of your scripts to catch common coding mistakes.
  • Module scripts: For modern applications, consider using <script type="module"> for ES6 module support.


Common Attributes

Attribute Description
src Specifies the URL of an external script file
async Downloads script in parallel and executes as soon as available
defer Downloads in parallel but executes after HTML parsing is complete
type Specifies the script type (e.g., "text/javascript", "module")
crossorigin Configures CORS requests for scripts

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...