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
Selected Reading
Should I include type=\'text/javascript\' in my SCRIPT tags?
The type="text/javascript" attribute in <script> tags is optional in modern HTML5. JavaScript is now the default scripting language for browsers, making this attribute unnecessary in most cases.
HTML5 and Modern Browsers
Since HTML5, browsers automatically assume JavaScript when no type attribute is specified. This simplifies script tags significantly.
Modern Approach (Recommended)
The current best practice is to omit the type attribute entirely:
<html>
<body>
<script>
console.log("Hello World!");
</script>
</body>
</html>
Output
Hello World!
Legacy Approach
Older HTML versions required explicit type declaration:
<html>
<body>
<script type="text/javascript">
console.log("Hello World!");
</script>
</body>
</html>
Output
Hello World!
When to Use type Attribute
Include type only for non-JavaScript content:
<!-- JSON data -->
<script type="application/json">
{"name": "John", "age": 30}
</script>
<!-- Template -->
<script type="text/template">
<div>{{name}}</div>
</script>
Comparison
| Approach | HTML Version | Required? | Recommendation |
|---|---|---|---|
<script> |
HTML5+ | No | Use this (modern) |
<script type="text/javascript"> |
HTML 4.01 | Yes (legacy) | Not needed anymore |
Conclusion
Omit type="text/javascript" in modern HTML5 development. Only include type when embedding non-JavaScript content like JSON or templates.
Advertisements
