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!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.4Shadow.com Ben Johnson

    Wait, so you’re saying IE doesn’t follow standards? I don’t believe it.

  • Ian

    IE doesn’t follow standards?

    Hahaha!

  • http://www.derosion.com Madmac

    Internet Explorer != the web.

  • http://www.sourceomnimedia.com PHPCamp.com

    Ha ha. IE is a four letter word when standards are involved. Nice article. Good info.

  • logic_earth

    test/javascript was never part of any standard infact Microsoft wasn’t the one who came up with it, Netscape did. Each browser that was made then on used text/javascript so here we are. Of course as with most things those who write the standards don’t go with the flow as the rest of the world.

    So how is IE not following the standard when text/javascript was never part of any standard and it was added just to obsolete it just a year or two ago. How is IE suppose to follow something that happened after it was made?

  • http://ian.sundermedia.com TheLunchBox

    @logic_earth

    Listen here logic, we’re in the middle of a good old fasioned IE bashing session. Let’s not go and end it just because IE didn’t start the problem.

    On a more serious note, I have to agree that this is an annoyance, but it’s minor when compared to the other inconsistencies among browsers. Unfortunately, as the competition among browsers increases, vendors will be more likely to add features that may not be supported by other browsers in order to differentiate themselves.

  • http://www.4Shadow.com Ben Johnson

    Situations like this make flash / flex appealing. It is not rocket science to read the W3C standards and build a browser to follow them. Almost every browser except IE has come close enough. Microsoft does a lot of these things purposely for reasons I will not get into.

    Logic, are you really going to go to battle for Microsoft and it’s capabilities to follow standards? If so you are a very brave man.

  • XLCowBoy

    *scratches head*

    So… why not use the correct type, and insert the IE one within conditional IE tags below it? (<!–[if IE] etc. sort of stuff)

    IE would then read the IE one, and the document would still validate, right?

  • http://webdesignstudent.net/ pritambarhate

    Why does Google ignore the standards?

    One more interesting thing, In the article you said:

    Google script Alex was embedding…

    I think here “Google script” is an important point. I have seen this time and again, scripts from Google are not confirming with the standards. If you will see HTML source for the home page of Google, it doesn’t even contain a DOCTYPE declaration. So this brings to the question:

    Why does Google ignore the standards?

    Generally you expect high quality work from Google. Or is it just high quality search results and sloppy code?

    Pritam Barhate
    ————————————
    WebDesignStudent.net

  • logic_earth

    I say we just use text/javascript because really it just doesn’t matter one bit. Every browser that has a small support for JavaScript understands text/javascript.

  • Anonymous

    The problem with standards authors is that they never want to go with the status quo. After all, they would not then be bringing in anything new.

    As mentioned previously, every major browser understands text/javascript.

    The fact that the standards authors don’t like it is just too bad.

    After all this is not only an IE thing.

    It is a standards committee versus the real world thing.

    Tail wagging the dog here.

    Oh, and yes, scripts from Google and company are often horribly broken any ways, irrespective of the enclosing script tags.

  • http://basicstate.com/ plumsauce

    The problem with standards authors is that they never want to go with the status quo. After all, they would not then be bringing in anything new.

    As mentioned previously, every major browser understands text/javascript.

    The fact that the standards authors don’t like it is just too bad.

    After all this is not only an IE thing.

    It is a standards committee versus the real world thing.

    Tail wagging the dog here.

    Oh, and yes, scripts from Google and company are often horribly broken any ways, irrespective of the enclosing script tags.

  • Chris

    Validating either of those is a waste of time. Does it help users or customers in any way, shape, or form? No. All adding the type attribute does is add to download time. It should be left out for any and all Javascript.

  • Anonymous

    Point one: Do you care about the document being valid, or passing a particular validation test?
    If the validator picks up on something silly, that doesn’t matter to anyone, then ignore it.

    Secondly: for most standards bodies, there must be at least one complete implementation before a proposal can gain acceptance. Clearly here things are all backwards: everyone uses ‘text/javascript’ and no-one uses ‘application/javascript’ so it clearly isn’t a proper standard, and should continue to be ignored.

  • Anonymous

    yup it was deffintily microsoft whatever it was im sure they are to blame – it has to be thier fault – dam that bill

  • http://garvinggarvin.com/ brainphat

    ‘Just came across this issue the other day. All of the grey areas is what makes this stuff so interesting to me. In this one issue you’ve got different camps, frustrated standards, vestigal usage, historical reasoning, and the ubiquitous IE exception.