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?
-
10Servers are rarely properly configured.Gumbo– Gumbo2010-04-24 22:01:23 +00:00Commented Apr 24, 2010 at 22:01
-
Also because you can put any type there, and if the browser is able to understand that type, it will execute the script.user216441– user2164412010-04-24 22:16:34 +00:00Commented Apr 24, 2010 at 22:16
-
@Gumbo: Really? Do you have some proof (i.e., some text containing statistics) for this?Marcel Korpel– Marcel Korpel2010-04-24 22:30:22 +00:00Commented Apr 24, 2010 at 22:30
-
1Also read stackoverflow.com/questions/189850/…Marcel Korpel– Marcel Korpel2010-04-24 22:31:47 +00:00Commented Apr 24, 2010 at 22:31
-
@Gumbo not surprising, given that browsers are historically lenient. Maybe they shouldn't have been?Roman Starkov– Roman Starkov2012-07-19 17:58:55 +00:00Commented Jul 19, 2012 at 17:58
4 Answers
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.
W3C did not adopt the
languageattribute, favoring instead atypeattribute 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.
9 Comments
type="text/javascript", though many servers will send the file with Content-type: application/javascript.text/javascript and text/ecmascript but marks them as "obsolete"type=baloney worked :)<!--[if IE 6]><script type=baloney>/* some evil crash code */</script><![endif]--> on my pages. en.wiktionary.org/wiki/…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?
Comments
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.
}
}
1 Comment
language attribute is deprecated: w3.org/TR/html401/interact/scripts.html#adef-languageIt 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.