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

avernet picture avernet · May 18, 2009 · Viewed 23.5k times · Source

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.

Answer

Quentin picture Quentin · May 18, 2009
  • text/javascript is obsolete
  • application/x-javascript was experimental while deciding to move to…
  • application/javascript is the current official MIME type for JS

That said, browsers often ignore the content-type sent by the server and pay a lot of attention to the type attribute (and some may not yet recognise application/javascript).

My recommendation:

  • Use application/javascript on the server
  • Use HTML 5 and omit the type attribute from script elements

NB: the HTML specification contradicts the MIME standard, and there is an effort to change it back to text/javascript so this may change in future.