The <script> tag in HTML is used to embed or reference executable code, usually JavaScript, within an HTML document.
Syntax of HTML <script> Tag
Using an internal <script> file
<script>
// JavaScript code write here
</script>
Using an external <script> file
<script src="script.js"></script>
Attributes of <script>
- src: Specifies the path to an external JavaScript file.
- type: Defines the type of script (default is "text/javascript").
- async: Loads the script asynchronously (doesn’t block HTML parsing).
- defer: Executes the script after the HTML document has been parsed.
- crossorigin: Handles CORS for external scripts.
- integrity: Provides security by checking the cryptographic hash of the script.
Placement of <script>
- Inside the <head>: Script loads before the page content.
- Just before </body>: Common practice to load scripts after content (improves speed).