views:

366

answers:

4

My understanding is that mime types are set by the web server. Why do we add the type="text/javascript or type="text/css" attribute? Isn't this a useless and ignored attribute?

+14  A: 

Because, at least in HTML 4.01 and XHTML 1(.1), the type attribute for <script> elements is required.

In HTML 5, type is no longer required.

In fact, while you should use text/javascript in your HTML source, many servers will send the file with Content-type: application/javascript. Read more about these MIME types in RFC 4329.

Notice the difference between RFC 4329, that marked text/javascript as obsolete and recommending the use of application/javascript, and the reality in which some browsers freak out on <script> elements containing type="application/javascript" (in HTML source, not the HTTP Content-type header of the file that gets send). Recently, there was a discussion on the WHATWG mailing list about this discrepancy (HTML 5's type defaults to text/javascript), read these messages with subject Will you consider about RFC 4329?

Marcel Korpel
+7  A: 

Douglas Crockford says:

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

He also says:

W3C did not adopt the language attribute, favoring instead a type attribute which takes a MIME type. Unfortunately, the MIME type was not standardized, so it is sometimes "text/javascript" or "application/ecmascript" or something else. Fortunately, all browsers will always choose JavaScript as the default programming language, so it is always best to simply write <script>. It is smallest, and it works on the most browsers.

For entertainment purposes only, I tried out the following five scripts

  <script type="application/ecmascript">alert("1");</script>
  <script type="text/javascript">alert("2");</script>
  <script type="baloney">alert("3");</script>
  <script type="">alert("4");</script>
  <script >alert("5");</script>

On Chrome, all but script 3 (type="baloney") worked. IE8 did not run script 1 (type="application/ecmascript") or script 3. Based on my non-extensive sample of two browsers, it looks like you can safely ignore the type attribute, but that it you use it you better use a legal (browser dependent) value.

brainjam
That's not conform the HTML spec: http://www.w3.org/TR/html401/interact/scripts.html#adef-type-SCRIPT
Marcel Korpel
Regarding the second part: read more about MIME types in RFC 4329: http://www.ietf.org/rfc/rfc4329.txt In fact, you really should specify `type="text/javascript"`, though many servers will send the file with Content-type: `application/javascript`.
Marcel Korpel
I really wonder what IE 6 will do with these examples. And yes, at least the 5th one (with `type` omitted) will work in that browser. That's because most browsers have fallback settings included, in case a document doesn't conform to the standards; that's no excuse to serve non-standard things or even tag soup.
Marcel Korpel
@Marcel Korpel, thanks for the links to the standards docs. I'm confused (or possibly not reading carefully): doesn't ietf.org/rfc/rfc4329.txt say *This document thus defines `text/javascript` and `text/ecmascript` but marks them as "obsolete"*
brainjam
@Marcel Korpel, IE6 accepts the same scripts as IE8, according to IETester.
brainjam
I was confused about this, too, the first time I saw it. It's just that the RFC people in their utter wisdom decided to standardize `application/javascript` in favour of `text/javascript`, but the former will just not work in some browsers (at least not in IE). Recently there was a discussion on the WHATWG mailing list about the default setting of `type` in HTML 5 vs. RFC 4329: http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2010-April/025813.html (also read the rest of the thread).
Marcel Korpel
@Marcel: I tested it in IE6, and only the `type=baloney` worked :)
Daniel Vassallo
@Daniel: Nice to know. :) From now on I will use `<!--[if IE 6]><script type=baloney>/* some evil crash code */</script><![endif]-->` on my pages. http://en.wiktionary.org/wiki/no_matter_how_thin_you_slice_it,_it%27s_still_baloney
Marcel Korpel
+2  A: 

It allows browsers to determine if they can handle the scripting/style language before making a request for the script or stylesheet (or, in the case of embedded script/style, identify which language is being used).

This would be much more important if there had been more competition among languages in browser space, but VBScript never made it beyond IE and PerlScript never made it beyond an IE specific plugin while JSSS was pretty rubbish to begin with.

The draft of HTML5 makes the attribute optional.

David Dorward
+5  A: 

Boris Zbarsky (Mozilla), who probably knows more about the innards of Gecko than anyone else, provided at http://lists.w3.org/Archives/Public/public-html/2009Apr/0195.html the pseudocode repeated below to describe what Gecko based browsers do:

if (@type not set or empty) {
   if (@language not set or empty) {
     // Treat as default script language; what this is depends on the
     // content-script-type HTTP header or equivalent META tag
   } else {
     if (@language is one of "javascript", "livescript", "mocha",
                             "javascript1.0", "javascript1.1",
                             "javascript1.2", "javascript1.3",
                             "javascript1.4", "javascript1.5",
                             "javascript1.6", "javascript1.7",
                             "javascript1.8") {
       // Treat as javascript
     } else {
       // Treat as unknown script language; do not execute
     }
   }
} else {
   if (@type is one of "text/javascript", "text/ecmascript",
                       "application/javascript",
                       "application/ecmascript",
                       "application/x-javascript") {
     // Treat as javascript
   } else {
     // Treat as specified (e.g. if pyxpcom is installed and
     // python script is allowed in this context and the type
     // is one that the python runtime claims to handle, use that).
     // If we don't have a runtime for this type, do not execute.
   }
}
Alohci
Just FYI, please note that the use of the `language` attribute is deprecated: http://www.w3.org/TR/html401/interact/scripts.html#adef-language
Marcel Korpel
What language is the above code?
Christopher Altman
@Christopher: "pseudocode"
Marcel Korpel