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:

  1. text/javascript
  2. application/javascript
  3. text/plain
  4. application/x-javascript

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:

no type

text/javascript

application/javascript

And what about spelling mistakes?:

txt/javascript

text/jvassscrippt

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.

Ian Bicking - 09 May 2011 [«« Reply to this]
IE9, with Compatibility View on (I assume emulating older IE version?) does not display the last example (application/javascript). It doesn't show document.write, it's just blank above the hr. Otherwise works fine. So no type is better than the "proper" modern type.
Matthew Schinckel - 10 May 2011 [«« Reply to this]
A non-javascript type is used, for instance, by the jQuery tmpl plugin. (text/x-jquery-tmpl, IIRC). This prevents it being executed as javascript, as it isn't.
Janet - 03 December 2013 [«« Reply to this]
Despite your conclusion 2.5 years ago, I can still see your html script containing "text/javascript". Any reason why you still keep it?
Peter Bengtsson - 03 December 2013 [«« Reply to this]
Excellent question! It must either be from old muscle memory habits or when copying a script tag from some other old piece of code.

Also, I think some tools like django_compressor which concatenates and merges multiple .js files into one still uses `type="text/javascript"` when it spits out the final result.

The other javascript I have here on my site is from other automated tools.


Your email will never ever be published