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:
src
attributeasync
or defer
)Here's the basic structure of a script element:
Or for external scripts:
This example shows a simple inline script that displays an alert when the page loads:
This example demonstrates loading JavaScript from an external file. The external script contains a function that changes the button's text when clicked.
The async
attribute allows the script to download in parallel with HTML parsing and execute as soon as it's available, without blocking.
This example shows how scripts can manipulate the DOM after the page loads:
This example demonstrates handling a button click event with JavaScript:
async
for independent scripts, defer
for scripts that depend on the DOM.'use strict';
at the beginning of your scripts to catch common coding mistakes.<script type="module">
for ES6 module support.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 |
The <style> HTML element contains style information for a document, or part of a document. ...
The <strong> element is used to indicate that its content has strong importance, seriousnes...
The <span> element is an inline container used to mark up a part of a text or document. Unl...
The <source> element is used to specify multiple media resources for media elements like &l...
The <small> element is used to represent side-comments and small print, typically for discl...
HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...