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