Umair Jabbar
Umair Jabbar

Reputation: 3666

What is an appropriate content-type header for JavaScript files?

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?

Upvotes: 53

Views: 70782

Answers (4)

You
You

Reputation: 23834

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.

Upvotes: 82

pooh17
pooh17

Reputation: 74

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

Upvotes: 0

Stefan van den Akker
Stefan van den Akker

Reputation: 7009

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

Who knows what it'll be in a year!

Upvotes: 7

Gordon
Gordon

Reputation: 317217

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).

Upvotes: 3

Related Questions