HTML4 HTML5
The <script> tag is used to embed or include external executable scripts in a web page – most commonly written in JavaScript[cite: 3]. It adds interactivity, handles events, manipulates document structure (DOM), or communicates with the server[cite: 3].
Both opening and closing tags are mandatory, even if the script is imported from an external file via the src attribute[cite: 3].
Attributes:
src – specifies the path (URL) to an external script file[cite: 3].async – instructs the browser to download the script asynchronously in the background and execute it immediately upon download, without blocking page rendering (applies to external scripts only)[cite: 3].defer – instructs the browser to download the script in the background but execute it only after parsing the entire HTML document (applies to external scripts only)[cite: 3].type – specifies the resource type (e.g., type="text/javascript" or type="module" when using ES6 modules)[cite: 3]. In modern HTML5, JavaScript is default and this attribute can be omitted[cite: 3].class, conditional id, and style are also used[cite: 3].In the example below, the script fetches a button from the document and displays a simple browser alert when clicked[cite: 3]:
Code:
The browser will interpret this code as follows (button will be active and respond to clicks)[cite: 3]: