Javascript – Do you really need to specify the type attribute?

csshtmljavascriptmime-typestags

Possible Duplicate:
Why write <script type=“text/javascript”> when the mime type is set by the server?

I read Dive into HTML5 a while back, and read its semantics chapter again just recently. I noted it advises not to use type="..." attributes on script and style, because:

  • The MIME type should be sent by the server,
  • JS and CSS are the defaults,
  • Browsers don't care.

However, I see it is still common practice to include type attributes (or, horror, language) on both script and style tags. Assuming the server is properly configured to send the correct MIME types, are there reasons for using these other than being explicit?

EDIT: This is explicitly about HTML5, not XHTML.

Best Answer

Most people are used to HTML 4/XHTML and before, where the type attribute is required for these elements.

In regards to HTML 5, these are indeed optional and the spec gives a default, depending on the element.

For the script tag, this defaults to text/javascript:

If the language is not that described by "text/javascript", then the type attribute must be present

For the style tag, this defaults to text/css:

The default value for the type attribute, which is used if the attribute is absent, is "text/css".

So, not needed, as you stated. However, browser support and server setups can't always be relied on - being explicit is a good idea as it avoids such problems.

And of course, not all browsers out there support HTML 5 - those that don't will use an earlier version where the attribute is required and your javascript/css might not get parsed in such browsers, meaning you end up with no CSS or javascript on older browsers, when a simple solution for backwards compatibility is to add the attribute.