Do you need text/javascript specified in your `
In HTML5, specifying type="text/javascript"
in <script>
tags is redundant. The MIME type default is JavaScript, so omit it for cleaner, modern code:
The dawn of JavaScript in HTML
Since the introduction of Netscape 2, JavaScript has been the default scripting language for web browsers. This longstanding convention means that modern browsers will automatically interpret script tags as JavaScript, even without the type
attribute. This streamlines code writing, resulting in cleaner, more efficient code.
When to use CDATA in XHTML
Though for HTML, type
is often redundant, XHTML documents can benefit from more attention. If your JavaScript code includes special HTML characters like <
or &
, then you should wrap the script within a CDATA section:
For non-JavaScript content, like templates or JSON data, the type
attribute can be leveraged to differentiate this data from executable scripts:
Farewell to old-fashioned constructs
Historically, <!--
HTML comments were used to hide scripts from ancient browsers that couldn't interpret <script>
tags. However, modern browsers have made these once-important constructs unnecessary. Similarly, HTML 4.01 required specifying a valid MIME type, such as text/javascript
, but this is no longer the case with HTML5.
When type="text/javascript" is still useful
Though generally superfluous for HTML5, some organizations may still keep the type="text/javascript"
convention for historical reasons or as a standard practice. However, a better reason to use type
today could be for organizing or managing scripts in complex applications.
Behind the scenes of minification
Web optimization and minification tools tend to strip away type="text/javascript"
declarations from scripts in pursuit of performance. This aligns with current standards, as the type
attribute offers no technical advantage.
Dig deeper - common practices and documentation
Official documentation and examples from some major entities still display the type
attribute, even though it's technically redundant. This could be due to outdated templates or a conscious preference towards explicitness. It's always a good idea to refer to the latest HTML standards and best practices to stay current.
When charset haunts you
Occasionally, you might encounter charset
in a <script>
tag. This attribute sets an explicit character encoding for the script file. While charset
might sound useful, modern best practices suggest encoding scripts in UTF-8, rendering charset
unnecessary. Phew!
Expert use of MIME types
For the academically curious, the official, IETF-approved MIME type for JavaScript is 'application/javascript' according to RFC 4329. However, in practice, altering MIME types in script tags is best reserved for when you're dealing with non-executable data like templates or JSON scripts.
Was this article helpful?