53

I am delivering a JS response from a PHP file. When setting the Content-Type header which value should I use, application/javascript or text/javascript?

What’s the difference between the two?

1

4 Answers 4

81

According to the IANA Registered MIME media type list, JS has two registered MIME types; the obsolete application/javascript and the now official text/javascript.

Thus, use text/javascript.

Note: This answer previously claimed the exact opposite (based on RFC 4329), but since RFC 9239 was published in May 2022 the text/javascript media type is preferred over application/javascript.

3
  • @some: Wikipedia implies otherwise, but this is the correct MIME type. The obsolete one may be used instead, if IE would choke in the correct one.
    – You
    Commented Oct 21, 2010 at 14:08
  • 2
    @some: I think, as per annevankesteren.nl/2006/05/javascript-mime-type, that application/javascript doesn’t work inside the type attribute in script tags in IE 7 and older, but is fine for your HTTP Content-Type header. I haven’t tested that though. Commented Apr 29, 2013 at 15:50
  • 8
    I believe this answer is obsolete. See the HTML5 spec which states “Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources”: html.spec.whatwg.org/multipage/… h/t stackoverflow.com/questions/21098865/… Commented Jul 25, 2021 at 22:38
7

According to IANA, on 2022-05-10, text/javascript should be used.

Who knows what it'll be in a year!

3

From Wikipedia on Internet Media Types

application/javascript: JavaScript; Defined in RFC 4329 but not accepted in IE 8 or earlier

There is also the deprecated text/javascript (which IE will probably not choke on).

0

you should only use text/javascript. It's the only MIME type guaranteed to work now and into the future. ~ MDN webdocs

top-voted answer to use application/javascript is obsolete

Not the answer you're looking for? Browse other questions tagged or ask your own question.