\n","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Anton Shumikhin","url":"https://explain.codes//author/anton-shumikhin"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-11-11T16:30:01.270Z","dateModified":"2024-11-11T16:30:02.808Z"}
Explain Codes LogoExplain Codes Logo

Do you need text/javascript specified in your `

javascript
prompt-engineering
best-practices
web-development
Anton ShumikhinbyAnton Shumikhin·Nov 11, 2024
TLDR

In HTML5, specifying type="text/javascript" in <script> tags is redundant. The MIME type default is JavaScript, so omit it for cleaner, modern code:

<script> // Gather 'round kids, it's pure JavaScript here! </script>

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:

<script>//<![CDATA[ var spinach = "<delicious>"; // Popeye was here 💪 //]]></script>

For non-JavaScript content, like templates or JSON data, the type attribute can be leveraged to differentiate this data from executable scripts:

<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "Person", "name": "Jane Doe", // Nice to meet you, Jane 👋 } </script>

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.