Loading...
Loading...
00:00:00

<script> Tag in HTML5

The <script> tag in HTML5 is used to include scripts, such as JavaScript, within a HTML document. Scripts allow you to add interactivity, dynamic behavior, and other advanced features to your web pages.

This tag is used to write the script in the html document or we can add external script file such as javascript.

Here's a basic example of how to use the <script> tag:

<script>
  alert("Hello, World!");
</script>
<!-- for external resources -->
<script src="example.js" ></script>

In this example, the script within the <script> tag displays an alert box with the message "Hello, World!".

Here are some of the most commonly used <script> tag attributes:

  1. src: Specifies the URL of an external script file to be included in the HTML document. If you use the src attribute, the script contents should not be included within the <script> tag.

  2. type: Specifies the type of script. The default value is "text/javascript", but you can also use other types such as "text/vbscript" or "text/ecmascript".

  3. async: Specifies whether the script should be executed asynchronously. If set to "async", the script will be executed as soon as it is available, without blocking the rest of the page.

  4. defer: Specifies whether the script should be executed after the page has finished loading. If set to "defer", the script will be executed in the order that it appears in the HTML document, after all other resources have been loaded.

  5. charset: Specifies the character encoding of the script. This attribute is used to ensure that the script is interpreted correctly, regardless of the character encoding of the HTML document.

It's important to note that scripts can have a significant impact on the performance of your web pages. To ensure that your web pages load quickly and efficiently, it's recommended to keep your scripts as small and optimized as possible, and to use the async or defer attributes to avoid blocking the rest of the page.

In conclusion, the <script> tag in HTML5 is a powerful tool that allows you to add interactivity, dynamic behavior, and other advanced features to your web pages. Whether you're using internal scripts or external script files, the <script> tag and its attributes provide a flexible and efficient way to include scripts in your HTML documents.