Do HTML5 Script tags need type="javascript"?
In HTML5 there's no need for the type attribute for JavaScript in <script> tags, since it's the default. The new norm is to omit type, as shown below:
Leaner syntax like this is totally compliant and recommended for coding in the modern Web world.
The evolution of web standards
We've reached a point where specifying "text/javascript" for the type attribute of <script> elements has been deemed as antiquated, largely due to the prevalence of JavaScript. This language has emerged as the dominant scripting language for the Web.
Browser behavior: Script execution protocol
Standard browsers work using a script execution protocol that assumes JavaScript as the default scripting language. Therefore, unless it's explicitly stated otherwise by a different MIME type, the inclusion of "text/javascript" is redundant.
Charset encoding: Are they necessary?
The need for charset declaration (<script charset="...">) within script tags has also become obsolete as the scripts are universally expected to be delivered in UTF-8 encoding. This is in alignment with the global transition towards UTF-8 as the primary character encoding for the Web.
When type can still hang around
While omission is the best practice, specifying type does no harm. There might be legacy systems or specific use cases that still require the attribute to function as expected. However, for most contemporary web development scenarios, it's safe to leave the type attribute out with confidence.
Best practices for script elements
Usage of external scripts
For external scripts, using the src attribute, culling the type="text/javascript" gives you:
This way, you avoid the equivalent of telling a pizza place you want a pizza.
Payload delivery: Inline scripts
Inline scripts also subscribe to this best practice. The simpler and uncluttered, the better:
Remember: a well-organized captain runs a tight ship.
Special script types for specific situations
There will be unique cases where non-standard script types like "text/babel" or "module" are mandatory. Such situations require explicit declaration of the type for ascertained functionality.
Was this article helpful?