A script tag's type in HTML5
9th of May 2011
If you look at html5boilerplate.com they never use any type on their <script> tags. Hmm... but is there more to it?
If you don't specify a type, the default becomes "text/javascript" but according to RFC4329 the "text/javascript" MIME type is obsolete in favor of "application/javascript".
If the default MIME type for a <script> tag thus becomes either "text/javascript" or "application/javascript" is there any sensible browser on this green earth that would not translate a piece of inline Javascript code as, exactly that; Javascript? Probably not.
What about <script> tags with a src attribute? Does the type matter?
I read the spec a couple of times and it feels like reading legalese but it ultimately says: the value of the type tag must be that of the body of the script tag.
So, what happens if you embed a javascript file with a mismatching type? Let's see:
I've created 4 different files all serve with a different Content-Type:
Now, let's see if we can get things wrong. And by wrong I mean that the javascript isn't executed and thus appearing as unexecuted raw Javascript code. First batch, embed them with different types:
And what about spelling mistakes?:
As you can probably see, the spelling mistakes stops the execution of Javascript.
Open any of them and see for yourself. All browsers that I've been able to test executes the javascript just fine in all of them. The all pass the w3c validator.
It would be nice if some Windows people can share if it works on Internet Explorer.
Conclusion
The type tag is probably not needed for either inline or embedded Javascript. It's a relic from the past that is just causing you extra typing strain.
The type attribute is there for you only for exceptional use cases. Not needed for general use and if you use it and accidentally spell it wrong it will break things.
Comment
Show all 2 commentsCommenting is currently disabled in Mobile version