Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why write <script type="text/javascript"> when the mime type is set by the server?

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?

like image 717
Christopher Altman Avatar asked Apr 24 '10 21:04

Christopher Altman


People also ask

Why do we write script type text JavaScript?

HTML <script> Tag. The HTML script tag <script> is used to embed data or executable client side scripting language in an HTML page. Mostly, JavaScript or JavaScript based API code inside a <script></script> tag. The following is an example of an HTML page that contains the JavaScript code in a <script> tag.

How does the server know the MIME type of a file?

When a browser receives a document from web-server, it reads content-type header and decides how to parse a file. I can explicitly set correct content-type for headers from scripts on server, but when web-server serves css or javascript files, it automatically sets correct mime-types.

What is script MIME type?

The MIME type for javascript wasn't standardized for years. It's now officially: "application/javascript". The real kicker here is that most browsers won't use that attribute anyway, at least not in the case of the script tag. They actually peek inside the packet and determine the type for themselves.

What is a MIME type and how does a Web server make use of it?

MIME types describe the media type of content, either in email, or served by web servers or web applications. They are intended to help provide a hint as to how the content should be processed and displayed. Examples of MIME types: text/html for HTML documents.


2 Answers

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.

like image 63
brainjam Avatar answered Sep 22 '22 23:09

brainjam


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?

like image 33
Marcel Korpel Avatar answered Sep 21 '22 23:09

Marcel Korpel