More articles

Should you stop adding the 'type' attribute in your script tags?

Written by
Published on

The year is almost 2020 as of this writing, and the HTML5 specification officially became a stable W3C Recommendation in 2014. Is it now time to stop using the beloved type attribute in your script tags? That's the magical question that we will tackle here today. And the answer isn't as simple as you might think. So do keep up as I navigate through various technologies, rules and committees on the search for this answer.

The HTML5 specification urges developers to omit the attribute as it is redundant in nature when used with the script tag. But most websites still have it in place (mainly because most large websites are pretty old). The official word from the W3C is stated as follows:

If the language is not that described by "text/javascript", then the type attribute must be present...The default, which is used if the attribute is absent, is "text/javascript".

Not exactly pushing you to remove it. But essentially, it's saying you can remove it.

Why it's difficult to remove

As mentioned above, most websites on the web are relatively old. The numbers are hard to come by, but from the looks of it "most" websites are not HTML5 certified. They were around before these new standards came into existence, and so they aren't wrong by including older elements in the sense that they came first. Older websites also tend to have many many more web pages, such as thousands. And paying a developer to go in and remove the 'type' attribute on every page seems like poor resource management

I myself have 'type' on almost every single page of this blog. Why is that? Because this blog is older than you think. And back in the day, we used the type attribute and we liked it. And believe me as much as I want to follow the newest guidelines and specifications, the muscle memory is real and I just like having the explicit type set. What if one day it changes and it's required again? I will be covered.

But going back to the older website discussion. The type attribute is required in HTML 4.01, the predecessor to HTML5. There is not default type set if you omit, causing unexpected issues I'm sure depending on the browser. The official specification states the following.

The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute[1].

When should you use it?

The HTML5 specification states that if the value is of 'text/javascript', then you should omit it as it is redundant. But what about when the type isn't set to that particular value. What else can it be set to you ask? The following is a list of MIME types that "can" be set. However, note that only the text/javascript type is the only one that is standardized and guaranteed to work.

All the others 'might' work on 'some' browsers. But why take the risk. So once again we ask, when should we use it? There's one other value that the type attribute can take on, and that is module.

What's a module?

Excellent question. JavaScript modules are more complex than I'd like to admit and deserve their own future blog post (coming soon). But essentially, JavaScript modules are a system of features that allow you to export and import content. Within a module, you can use the export keyword to export just about anything. You can export a const, a function, or any other variable binding or declaration.

And you can then use the import keyword later on to import that module from another module. Current technologies such as Node.js have made use of modules since their inception. So they aren't anything new.

Working on older websites

The type attribute is there for a reason of course. It's there to tell the browser that this particular script is written in JavaScript. The answer here is that if you are working on a pre-2014 website depending on the particular HTML level, you might need to continue to include it. This happens more often that you would think.

Large organizations, such as Google and Microsoft, can for sure work at staying up to date on the latest specifications and standards. But most websites on the web are not Google or Microsoft.

Even the W3's very own website doesn't quite match up just ye and is running on 4.01.

So if you are working on their website, then do continue to include the type attribute please. But if you working on a brand new HTML5 project and want to follow the latest standards, feel safe in knowing that the browser default has got you covered.

I hope you enjoyed this deeper look at scripts, specifications and standards as I attempt to unravel the mysteries of the web development world. Comment down below if you are like me and prefer to include the type tag, or if you are keeping up to date with the standards.

- Walt
thatsoftwaredude

Land your next big coding job. Search through 1000's of job listings.
X

Discussion / Comments / Questions

No messages posted yet

Add a comment

Send me your weekly newsletter filled with awesome ideas
Post comment