What Is The HTML <script> Tag?

What Is The HTML Script Tag

Date First Published: 28th February 2023

Topic: Web Design & Development

Subtopic: Web Development

Article Type: Computer Terms & Definitions

Difficulty: Medium

Difficulty Level: 5/10

Learn more about what the HTML <script> tag is in this article.

In HTML, the <script> tag is used to embed client-side JavaScript code onto a webpage. It can point to an external script file using the src attribute or contain scripting statements. Common uses for JavaScript are form validation, setting of cookies, and displaying animations. The <script> tag can be placed in the <body> or <head> section of a HTML document and multiple script tags can be placed in one document as the browser will run all the scripts, starting from the first script tag to the last one.

Example Of The <script> Tag

An example of JavaScript code embedded within the <script> and </script> tags can be seen below:

<!DOCTYPE html> <html> <head> <title>Simple JavaScript Example</title> </head> <body> <h1>Simple JavaScript Example</h1> <p>Below is an example of a simple JavaScript function that divides two numbers.</p> <script> alert(300 / 5) </script> </body> </html>

The output of the HTML document with the JavaScript code can be seen below.

JavaScript Example Screenshot

Attributes

Below are the attributes for the <script> tag.

Attribute Description
async Specifies that the external script will be run asynchronously.
charset Specifies the character encoding used with an external script file.
defer Specifies that the external script will be run once the page has finished parsing.
src Specifies the URL of an external script file.
type Sets the media type of the script.
Src Specifies the URL of the audio file.

Note: Info Icon

HTML5 does not require the 'type' attribute as the default language is JavaScript in the <script> tag. But, it is still required in HTML4.


Feedback

  • Is there anything that you disagree with on this page?
  • Are there any spelling, grammatical, or punctuation errors on this page?
  • Are there any broken links or design errors on this page?

If so, it is important that you tell me as soon as possible on this page.


Comments