- HTML Tutorial
- HTML - Home
- HTML - Introduction
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- 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 Color Names & Values
- HTML - Color Names
- 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 Picker
- HTML References
- HTML - Cheat Sheet
- 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
HTML <script> tag is used to declared Client-side script (JavaScript). The tag may either contain a link to an external file containing scripts or the script itself.
The script tag is employed for defining a client-side script for image manipulation, form validation, and dynamic content updates. 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
<script> .... </script>
Attribute
HTML script tag supports Global and accepts some specific attributes as well which is listed below.
Attribute | Value | Description |
---|---|---|
async | async | Specifies that the script is executed asynchronously. |
crossorigin | anonymous use-credentials |
Defines the mode of the request to an HTTP CORS request. |
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. | |
nomodule | true false |
Specifies that the script should not be executed in browsers supporting ES6 modules |
integrity | filehash | Allows browser to check fetched script to ensure the coode is never loaded if the source has been manipulated. |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin same-origin strict-origin strict-origin-when-cross-origin unsafe-url |
Specifies which referrer information to send when fetching a script |
src | Specifies a URI/URL path of an external script. | |
type | Specifies the scripting language as a content-type (MIME type). |
Examples of HTML script Tag
Bellow examples will illustrate the usage of script tag. Where, when and how to use script tag.
Internal JavaScript using script Tag
Let’s look into the basic example on the usage of the <script> tag. Here we have used javascript code Internally through 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>
External JavaScript using script Tag
Consider another scenario where we are going to mention the <script> tag outside the <body> tag.
<!DOCTYPE html> <html> <head> <script src="index.js" type="text/script"></script> </head> <body> <h1>TutorialsPoint</h1> </body> </html>
// File Name- index.js document.write("You're visiting tutorialspoint!")
Using Multiple script Tag
Following is the example, where we are going to use multiple <script> tag for each code of JavaScript. This code will generate an output, making the script tag trigger, display an alert twice, and display text on the webpage.
<!DOCTYPE html> <html> <head> <script> alert('WELCOME TO') </script> </head> <body> <h2>The Best E-Way Learning.!</h2> <script> alert('TUTORIALSPOINT') </script> </body> </html>
Supported Browsers
Tag | |||||
---|---|---|---|---|---|
script | Yes | Yes | Yes | Yes | Yes |