Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the types of tags involved in javascript?
In JavaScript, there are several types of HTML tags that work with JavaScript code. The most important is the <script> tag, which is essential for embedding or linking JavaScript in web pages.
HTML <script> Tag
The <script> tag is used to define client-side scripts in HTML documents. It can contain JavaScript code directly or reference an external JavaScript file. All JavaScript code must be placed within <script> tags to be executed by the browser.
Types of Script Tags
Inline JavaScript
JavaScript code written directly inside the <script> tag:
<html>
<body>
<p id="demo"></p>
<script>
var a = 5;
var b = 10;
var sum = a + b;
document.getElementById("demo").innerHTML = "Sum: " + sum;
</script>
</body>
</html>
Sum: 15
External JavaScript
Linking to an external JavaScript file using the src attribute:
<html>
<head>
<script src="script.js"></script>
</head>
<body>
<p id="external-demo"></p>
</body>
</html>
Script Tag Placement
Script tags can be placed in different locations:
- Head section: Loads before page content
- Body section: Loads with page content
- End of body: Loads after page content (recommended)
Other JavaScript-Related Tags
<noscript> Tag
Displays content when JavaScript is disabled in the browser:
<script>
document.write("JavaScript is enabled!");
</script>
<noscript>
<p>JavaScript is disabled in your browser.</p>
</noscript>
Script Tag Attributes
| Attribute | Description | Example |
|---|---|---|
type |
Specifies script type | type="text/javascript" |
src |
External script file URL | src="app.js" |
async |
Load script asynchronously | async |
defer |
Defer script execution | defer |
Conclusion
The <script> tag is the primary way to include JavaScript in HTML. It can contain inline code or reference external files, with placement affecting when the code executes.
