- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - <script> Tag
Client-side script (JavaScript) is declared in an HTML document using the <script> tag. The script tag is employed for defining a client-side script for image manipulation, form validation, and dynamic content updates. The tag may either contain a link to an external file containing scripts or the script itself. The src attribute allows you to specify the location of an external file.
The HTML <script> tag can be placed in both the <head> and <body> elements. Although the location of a script within an HTML document does not affect how it is performed, the scripts that must run first must be placed in the document's heading. An HTML document can contain numerous instances of the <script> tag.
Syntax
Following is the syntax of <script> tag −
<script> .... </script>
Specific Attributes
The HTML <picture> tag also supports the following additional attributes −
S.No. | Attribute & Description |
---|---|
1 | async Specifies that the script is executed asynchronously. |
2 | charset Defines the character encoding that the script uses. |
3 | defer Declares that the script will not generate any content. Therefore, the browser/user agent can continue parsing and rendering the rest of the page. |
4 | src Specifies a URI/URL of an external script. |
5 | type Specifies the scripting language as a content-type (MIME type). |
Example
Let’s look into the basic example on the usage of the <script> tag.
<!DOCTYPE html> <html> <head> <title>HTML script Tag</title> </head> <body style="background-color:#EAFAF1 "> <script type = "text/JavaScript"> document.write("You're visiting tutorialspoint!") </script> </body> </html>
When we execute the above code, it will generate an output consisting of script text displayed on the webpage.
Example
Consider another scenario where we are going to mention the <script> tag outside the <body> tag.
<!DOCTYPE html> <html> <head> <script> document.write("The Best E-Way Learning.!"); </script> </head> <body style="background-color:#D2B4DE "> <h1>TUTORIALSPOINT</h1> </body> </html>
On running the above code, an output window will pop up consisting of the text along with the script tag text displayed on the webpage.
Example
Following is the example, where we are going to use multiple <script> tag.
<!DOCTYPE html> <html> <head> <script> alert('WELCOME TO') </script> </head> <body> <h2>The Best E-Way Learning.!</h2> <script> alert('TUTORIALSPOINT') </script> </body> </html>
When we execute the above code, it will generate an output, making the script tag trigger, display an alert twice, and display text on the webpage.
Example
Following is an example where we are going to make modifications to the text using the <script> tag.
<!DOCTYPE html> <html> <body> <h1>BUGATTI CHIRON</h1> <p id="tutorial">The CHIRON is the fastest, most powerful, and exclusive production super sports car in BUGATTI’s history.</p> <script> function mytutorial() { document.getElementById("tutorial").style.fontSize = "25px"; document.getElementById("tutorial").style.color = "#DE3163"; document.getElementById("tutorial").style.backgroundColor = "#D5F5E3"; } </script> <button type="button" onclick="mytutorial()">Click Me!</button> </body> </html>
On running the above code, the output window will pop up, displaying the text along with a click button on the webpage. When the user clicks on the button, the event gets triggered and applies CSS to the text.