HTML - <script> Tag



The HTML <script> tag is used to declare Client-side script (JavaScript). The tag may either contain a link to an external file with scripts or the script itself.

The <script> tag is used to define client-side script for image manipulation, form validation, and dynamic content updates. The src attribute specifies the location of an external file.

The HTML <script> tag can be placed in both <head> and <body> elements. Although the script's location within an HTML document does not affect its execution, scripts that must run first should be placed in the document's heading. An HTML document can contain multiple instances of the <script>.

Syntax

Following is the syntax of <script> tag −

<script> .... </script>

Attributes

The HTML <script> tag supports Global and accepts specific attributes, which is are listed below.

Attribute Value Description
async async Specifies that the script executes asynchronously.
crossorigin anonymous
use-credentials
Specifies the mode of the request for an HTTP CORS request.
defer Specifies that the script will not generate any content, allowing the browser or user agent to 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 the browser to check the fetched script to ensure the code 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 the URI/URL path of an external script.
type Specifies the scripting language as a content-type (MIME type).

Example: Internal JavaScript Using <script> Tag

Lets look at a basic example of using the <script> tag. In this example, we use internal JavaScript code within the <script> tag. This HTML code displays the message.

<!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>

Example: External JavaScript Using <script> Tag

Consider another scenario where we place the <script> tag outside the <body> tag. This HTML code links to an external JavaScript file, "index.js", and displays the heading "TutorialsPoint" on the webpage.

<!DOCTYPE html>
<html>
<head>
   <script src="index.js" type="text/script"></script>
</head>
   <body>
   <h1>TutorialsPoint</h1>
</body>
</html>

Example: Multiple <script> Tags

In the following example, where we are going to use multiple <script> tag for each JavaScript code. This code will generate an output, triggering the script tag to display two alerts and text on the webpage. This HTML code displays two alert messages.

<!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 Chrome Edge Firefox Safari Opera
script Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements