A script tag's type in HTML5

10 May 2011   4 comments   Javascript

Mind That Age!

This blog post is 6 years old! Most likely, it's content is outdated. Especially if it's technical.

Powered by Fusion×

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



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.


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
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
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.
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
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.
Thank you for posting a comment

Your email will never ever be published

Related posts

maxlength_countdown() - a useful jQuery plugin for showing characters left 01 May 2011
Test static resources in Django tests 02 June 2011
django-html-validator 20 October 2014
How to use premailer as a command line script 13 July 2012
Difference between $.data('foo') and $.attr('data-foo') in jQuery 10 June 2012
Going real simple on HTML5 audio 14 October 2011
EmailInput HTML5 friendly for Django 02 August 2011
RequireJS versus HeadJS 09 January 2011