HTML Tutorial
The <script>
tag is used to embed scripts into an HTML document. This allows developers to add dynamic functionality to their webpages.
<script>
:src
attribute to specify the location of the external script file.<script>
tag using the content between the opening and closing tags.defer
or async
attributes to indicate how the script should be loaded. defer
loads the script after the page has parsed, while async
loads it concurrently.<script>
:Attribute | Description |
---|---|
src | Location of the external script file |
type | Specify the script's MIME type (e.g., "text/javascript") |
defer | Load the script after the page has parsed |
async | Load the script concurrently |
integrity | Specify the integrity value to validate the script's authenticity |
charset | Define the character encoding of the external script file |
<script>
:External Script:
<script src="my_script.js"></script>
Inline Script:
<script>
// Your JavaScript code here
</script>
Exploring the <script>
Tag:
Consider the following HTML example:
<!DOCTYPE html>
<html>
<head>
<title>My Webpage</title>
<script src="my_script.js"></script>
</head>
<body>
<h1>Welcome to my webpage!</h1>
<button onclick="greetUser()">Greet User</button>
<script>
function greetUser() {
alert("Hello there!");
}
</script>
</body>
</html>
In this example:
<script src>
tag.greetUser()
.onclick
attribute calls greetUser()
when clicked, displaying an alert.This demonstration showcases the versatility of the <script>
tag for enhancing the functionality of webpages.