HTML <script> Tag
The HTML script tag <script> is used to embed data or executable client side scripting language in an HTML page.
In the above example, a
alert('Hello, how are you?') that display a message box.
HTML v4 requires the
type attribute to identify the language of script code embedded within script tag.
HTML v5 page does not require the
An HTML page can contain multiple
<script> tags in the
The browser executes all the script tags, starting from the first script tag from the beginning.
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.
Above, the first
<script> tag containing
The browser loads all the scripts included in the
<head> tag before loading and rendering the
<body> tag elements.
<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.
Reference the External Script File
<script> tag can also be used to include an external script file to an HTML web page by using the
.js extension and include it in a web page using <script> tag and reference the file via src attribute.
src attribute is the path or url from which a file needs to be loaded in the browser.
Note that you can load the files from your domain as well as other domains.
<script> can contain the following global attributes: