Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

HTML script Tag

The HTML script tag <script> is used to embed data or executable client side scripting language in an HTML page. Mostly, JavaScript or JavaScript based API code inside a <script></script> tag.

The following is an example of an HTML page that contains the JavaScript code in a <script> tag.

Example:
<html>
<head>
</head>
<body>
<h1> JS Tutorials</h1>
<script>
alert('Welcome to Soopro Pathshala');
</script>
</body>
</html>

HTML v4 requires the type attribute to identify the language of script code embedded within script tag. This is specified as MIME type e.g. 'text/javascript', 'text/ecmascript', 'text/vbscript', etc.

HTML v5 page does not require the type attribute because the default script language is 'text/javascript' in a <script> tag.

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

Scripts without async, defer or type="module" attributes, as well as inline scripts, are fetched and executed immediately, before the browser continues to parse the page. Consider the following page with multiple script tags.

Example:
<html>
<head>
<script>
alert('JavaScript 1');
</script>
</head>
<body>
<h1> JavaScript Tutorials</h1>
<script>
alert('JavaScript 2');
</script>
<p>This page contains multiple script tags.</p>
<script>
alert('JavaScript 3'); </script>
</body>
</html>

Above, the first <script> tag containing alert('JavaScript 1') will be executed first, then alert('JavaScript 2') will be executed, and then alert('JavaScript 3') will be executed.

The browser loads all the scripts included in the <head> tag before loading and rendering the <body> tag elements. So, always include JavaScript files/code in the <head> that are going to be used while rendering the UI. All other scripts should be placed before the ending </body> tag. This way, you can increase the page loading speed.