43

Possible Duplicate:
When serving JavaScript files, is it better to use the application/javascript or application/x-javascript

what is difference between text/javascript and application/javascript?

As IE will completely ignore the script tag if you have set type attribute to application/javascript.

But what is the main difference and in which case we need to use the particular.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Avinash
  • 6,064
  • 15
  • 62
  • 95
  • 4
    http://stackoverflow.com/questions/876561/when-serving-javascript-files-is-it-better-to-use-the-application-javascript-or – stecb May 25 '11 at 10:24

2 Answers2

63

text/javascript was introduced when the web was young and people hadn't thought things through.

Then people thought things through, and decided that text/* should be reserved for things designed to be human readable (which is why some XML is text/xml and other XML is application/xml). JavaScript is not human readable, so text/javascript was deprecated and application/javascript was introduced to replace it.

Years later, some browsers still haven't caught up.

You can configure your server to always serve application/javascript in the HTTP headers; browsers that don't support it also pay no attention to the actual content-type.

For the time being, if you are writing HTML 4 or XHTML 1, specify text/javascript in the type attribute for the sake of backwards compatibility. If you are writing HTML 5, then omit the type attribute (as it is now optional).

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 3
    You make it compatible by doing what I said in the answer. – Quentin May 25 '11 at 10:30
  • It is specific to the server, and it probably is another question, but one for Server Fault rather then Stack Overflow (although setting up MIME/Content types should be fairly easier to figure out from the manual) – Quentin May 25 '11 at 10:56
  • @Thayne — "Human readable" in this context means "You can ignore the tags and read it as English (or French or whatever)". It doesn't mean "It text that you can interpret in your head if you understand the programming language". – Quentin Jul 24 '18 at 09:40
  • 1
    Why is CSS considered "human readable" but not Javascript? As far as I know, I've never seen `application/css`, only `text/css`. – Donald Duck Jul 23 '19 at 23:49
9

HTML 4.01 (1999) specification suggests using MIME type text/javascript (http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#h-18.2.2.2).

However, RFC 4329 (2006) now recommends the use of application/javascript.

It seems that historically text/javascript was used a lot and since it was the type that browsers most likely supported, this was the type that got suggested also in the HTML specification. Ideally, you would use application/javascript.

In practice you may need to use text/javascript to provide compatibility with less-conforming browsers.

jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • That's an example (rather than a recommendation), and one that predates the change in the IANA registered mime type to application/javascript. – Quentin May 25 '11 at 10:26
  • HTML 4.01. See the edit! But yeah agreed, recommends is not the right word (more like suggests or implies). But I agree, this implication is somewhat outdated. – jsalonen May 25 '11 at 10:27