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

JavascriptHttpContent Type

Javascript Problem Overview


The whole question fits in the title. And to add some context: I'm not asking what is the best according to what the specs are saying, but rather what works the best given the mix of browsers deployed nowadays.

Some data points:

  • Google uses text/javascript for the JS used on their home page.

  • Google uses text/javascript on Google Docs.

  • Google uses application/x-javascript to serve JavaScript files with their Ajax libraries service.

  • Yahoo uses application/x-javascript to serve their JS.

  • Yahoo uses application/x-javascript for the JavaScript served on their home page.

Javascript Solutions


Solution 1 - Javascript

According to the IETF's ECMAScript Media Types Updates as of 22 February 2021, the RFC-4329 is obsolete.

Therefore:

  • text/javascript is a recommended standard (both by IETF and by MDN)
  • application/x-javascript was experimental while deciding to move to…
  • application/javascript is obsolete

Solution 2 - Javascript

In most situations, the mime type the server sends makes no practical difference. I would go with application/javascript, which is also recommended by an RFC.

Solution 3 - Javascript

Here's the 2020 answer to this question.

text/javascript is the correct JavaScript MIME type per the HTML Standard, which states:

> Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources, and must not use non-JavaScript MIME types.

And also:

> […] the MIME type used to refer to JavaScript in this specification is text/javascript, since that is the most commonly used type, despite it being an officially obsoleted type according to RFC 4329.

Work is underway to reflect this reality in an RFC at the IETF level: https://datatracker.ietf.org/doc/draft-ietf-dispatch-javascript-mjs/

Any claim that "text/javascript is the obsolete one" is saying so based on RFC 4329, which both the HTML Standard and the abovementioned IETF draft (i.e. an upcoming RFC) are explicitly correcting.

Solution 4 - Javascript

If you choose to use application/javascript for js in your pages, IE7 and IE8 will not run your script! Blame Microsoft all you want, but if you want most people to run your pages use text/javascript.

Solution 5 - Javascript

It used to be language="javacript". Then it changed to type="text/javascript". Now it is type="application/javacript". Ok this is getting dumb. Some of the older browsers don't recognize the new application/javascript, but still recognize the older text/javascript. I plan to continue using this, or else I'll waste hours of my time trying to change EVERY instance of text/javascript into application/javascript.
Now some day the opposite might be true. Some day the newest browsers might reject the old technique in order to be strictly standard's compliant.
But until people viewing my website start complaining that "ever since upgrading my browser, about 50% of your website disappeared", I have no motive to change the code in my website.

Solution 6 - Javascript

It has been "text/javascript" but this is obsolete (see the IANA List) and now it should be "application/javascript" (see another IANA List).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionavernetView Question on Stackoverflow
Solution 1 - JavascriptQuentinView Answer on Stackoverflow
Solution 2 - JavascriptMatthew FlaschenView Answer on Stackoverflow
Solution 3 - JavascriptMathias BynensView Answer on Stackoverflow
Solution 4 - JavascriptDrew BView Answer on Stackoverflow
Solution 5 - JavascriptanonView Answer on Stackoverflow
Solution 6 - JavascriptTim BütheView Answer on Stackoverflow