HTML script tag

<script> tag in HTML

The <script> tag allows users to insert client-side scripts or data blocks in a document.

Scripts may be designed to provide interactivity, calculate and display values or run animations and other tasks.


If the below examples are difficult to understand, leave it, we will see about it in detail during the JavaScript Tutorials.

Example

Try Yourself

Attributes of <script> tag

  • src
  • async
  • crossorigin
  • defer
  • integrity
  • nomodule
  • nonce
  • type

src attribute in <script> tag

src attribute is be used to add an external scripts file to the current web page.

Example

Try Yourself

In the above example, sample.js script file is imported, Click here to see that script file.


async attribute in <script> tag

async attribute instructs the browser to execute the script first, and after completely executing the script the other process starts to load.


In the below example, the <h1> element will appear after closing the alert box.

Example 1 - Without async attribute

Try Yourself

In the below example, the <h1> element will appear simultaneously with the alert box.

Example 2 - With async attribute

Try Yourself