JavaScript - - By Andrew Tetlaw

JavaScript MIME Type: Damned if You Do, Damned if You Don’t!

In an article he posted a little while back, Alex Walker mentioned some trouble he had with <script> tags when trying to add the type attribute. The Google script Alex was embedding had no type attribute but wouldn’t work when he added one. Douglas Crockford has suggested in his Advanced JavaScript videos that we drop the type attribute altogether since browsers default to JavaScript anyway. The problem is under HTML 4.01 and XHTML 1.0 the type attribute is required. So if you care about validation, like Alex does, then you’ll want to add it.

But, what is the correct type value for JavaScript? The other reason Mr Crockford provides for dropping the attribute is that that the value most people use, text/javascript, is wrong! It’s obsolete, according to rfc4329. This is also confirmed by Anne van Kesteren who has covered this issue already (way back in May of 2006 – yes Anne is one of the gurus and I am not), as well as the SitePoint HTML Reference.

The correct type value for JavaScript is actually application/javascript. But wouldn’t you know it, Internet Explorer will not execute the code if you use that type value. So here’s a hell of a situation, to produce a valid HTML or XHTML document with script tags, we are required to use a type attribute, but to get it to function across all browsers we have to use an incorrect value.

Just goes to show, the web is really still only an alpha version. Can’t wait for the first beta!

Sponsors
Login or Create Account to Comment
Login Create Account