HTML Tutorial



HTML script tag


HTML <script> Tag

The <script> tag is used to define client-side JavaScript. It allows you to add interactive features like alerts, form validations, and dynamic content changes directly into your HTML page.


Purpose of <script> Tag

JavaScript adds logic and interactivity to a website. The <script> tag either includes JavaScript code directly or references an external JavaScript file.


Syntax

<script>
// JavaScript code here
</script>

OR

<script src="script.js"></script>
  

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Script Example</title>
    <script>
      function greet() {
        alert("Welcome to the website!");
      }
    </script>
  </head>
  <body onload="greet()">
    <h1>Hello User</h1>
  </body>
</html>
  

Attributes

  • src: Specifies the path to an external JavaScript file.
  • type: Specifies the scripting language (default is "text/javascript").
  • async: Loads the script asynchronously.
  • defer: Defers the execution of the script until after the document has been parsed.

Conclusion

The <script> tag is essential for adding dynamic and interactive features to your web pages using JavaScript. It can be used inline or to load scripts from external files.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review