HTML script tag

What is the HTML <script> tag, and how is it used to add JavaScript to a webpage?

Through HTML you can create static webpages. HTML documents also called static piece of content. So if you want to create dynamic webpages and want to improve your webpages design and functionality, for it you can use scripting languages such as VBScript, JavaScript etc. Through <script> tag you can include script. If you want to include JavaScript so you can do it with below mentioned syntax:


<script language="JavaScript" type="text/JavaScript">
  <!-- JavaScript Statements here -->
</script>
If you want to include VBScript so you can do it with below mentioned syntax:

<script language="VBScript" type="text/vbscript">
  <!-- VBScript Statements here -->
</script>
Attribute Description
Language You can provide scripting language name as its value. As mentioned below:

language="JavaScript" or language="VBScript"
Type You can specify type attribute as mentioned below:

type="text/JavaScript"
Src If you want to include any external JavaScript file so you can include through src attribute. Src stands for source which required the path of your JavaScript file which you want to include.

src="myJavaScript.js"
JavaScript Example

<html>
  <head>
    <title>JavaScript</title>
  </head>

  <body>
    <script language="JavaScript" type="text/JavaScript">
      document.write("JavaScript is very easy to learn");
    </script>
  </body>
</html>
VBScript Example

<html>
  <head>
    <title>VBScript</title>
  </head>

  <body>
    <script language="VBScript" type="text/VBScript">
      MsgBox "Hello World"
    </script>
  </body>
</html>

FAQ 1. What is the difference between the async and defer attributes? Both attributes allow external JavaScript files to download without stopping HTML parsing during the download. The async attribute executes the script as soon as it becomes available, while defer waits until HTML parsing is complete and preserves the order of deferred scripts. 2. Can I use multiple script tags in one HTML document? Yes, you can use multiple script tags in a single HTML document. This allows you to include different JavaScript files or write separate blocks of JavaScript for different webpage functions. 3. What happens if I place a script tag inside the head section? When a normal script tag is placed inside the head section, the browser generally executes its JavaScript before continuing to parse the remaining HTML. This may delay page rendering and can cause problems if the script tries to access elements that have not been created yet.