127

What is the MIME type of javascript?

More specifically, what is the right thing to put in the "type" attribute of a script tag? application/x-javascript and text/javascript seem to be the main contenders.

1

5 Answers 5

154

This is a common mistake. The MIME type for javascript wasn't standardized for years. It's now officially: "application/javascript".

The real kicker here is that most browsers won't use that attribute anyway, at least not in the case of the script tag. They actually peek inside the packet and determine the type for themselves.

So the bottom line is that the type="text/javascript" doesn't do anything as far as the javascript is concerned, but it's part of the spec for both HTML 4 and XHTML 1.0.

9
  • 40
    I can confirm that having <script type="application/javascript"> will fail in Internet Explorer. Commented Oct 10, 2008 at 2:23
  • 4
    Standards are a good thing but some times the choices made by these committees are baffling. Javascript is text, text/javascript is what is in use so why choose application/javascript. Sometimes I wonder whether these people actually live in the real world. ;) Commented Oct 10, 2008 at 7:02
  • 3
    @AnthonyWJones: It's complicated. This is an example where the standard came through to clean up a wild mess of rampant practices. There were as many as 8 ad-hoc MIME types in use. The committee came back with a solid recommendation, but only too late. And while JS is text, it's also source code.
    – keparo
    Commented Oct 12, 2008 at 5:59
  • 5
    Browsers won't do anything ... but the mime type can mean a lot to a proxy server or a CDN (like Akamai), these might do some last mile compression of known mime types for you. If headers are clean and standard, such things are easier. Commented Aug 20, 2009 at 19:49
  • 4
    According to IETF's ECMAScript Media Types Updates, RFC 4329 is obsolete.
    – Mike
    Commented Apr 13, 2021 at 17:55
32

Far out this is the first page I've found on the topic with any sense about it.

My collective research suggests:

  1. text/javascript as Keparo stated must be used in html4 and xhtml1 if you want it to validate. Though browsers ignore it.
  2. application/javascript is expected to be the new official MIME type if everyone agrees and when everything catches up.
  3. application/x-javascript (x meaning unofficial) is the current server side MIME reference for javascript.
  4. Everyone expects that as per usual, Microsoft will decide to do something completely different to further confuse and stuff up the matter.

Summary: For now, if you want your html/xhtml to work in MSIE and validate with W3C then declare type="text/javascript". If you want your web server to know that you mean javascript then use application/x-javascript.

4
  • 3
    +1 for mentioning server-side JS. However, have you a source or quote, who uses app/x-js on server side?
    – Boldewyn
    Commented Jan 4, 2010 at 9:40
  • Apache httpd.conf uses server side javascript MIME to configure things like: - - ForceType (For non-suffixed or non-standard files), - Output Filters (Like minifiers, gzip, compress, and anything else that needs to ID data type by MIME). Also, Id give a -1 to anyone using server side javascript, and the advise: get serious and dont be afraid to learn.
    – ekerner
    Commented Jan 12, 2010 at 0:25
  • Another SS javascript MIME requirement example is the case where your using a server side database interface script/program to dynamically generate your javascript code while populating variables within from a database of some kind. The generating code must declare 'Content-type: application/x-javascript' as a header, otherwise the server software - and perhaps even client software (if not explicitly declared) - will have no way of recognizing the data as javascript (As in my previous comment).
    – ekerner
    Commented Jan 12, 2010 at 3:36
  • 4
    lol looking at my comment above, server side js in 2010 seemed like a joke and in the last 10 yrs nodejs made me eat my words haha
    – ekerner
    Commented Oct 7, 2020 at 22:36
6

text/javascript

I believe IE doesn't accept application/x-javascript

Specifying the scripting language

4

In a script tag I would use text/javascript. This appears in the HTML 4.0 specification, anyway.

Funny how the RFC that standardized on application/javascript is of 2006, but text/javascript is still more common. Is this yet another case of custom triumphing over standards? It also appears in HTML5 and could be because of the same reason PNG images are avoided - compatibility with IE 5 and 6.

2
  • 1
    And because intuatively it makes more sense to call it text/javascript. Commented Oct 10, 2008 at 7:03
  • 4
    Intuition varies. "application/" != "binary/"
    – outis
    Commented Apr 7, 2011 at 20:23
0

The official RFC that defines the Javascript MIME Type is RFC4329.

     
7.  JavaScript Media Types

7.1.  text/javascript (obsolete)

   Type name:               text
   Subtype name:            javascript
   Required parameters:     none
   Optional parameters:     charset, see section 4.1.
   Encoding considerations:
      The same as the considerations in section 3.1 of [RFC3023].

   Security considerations: See section 5.
   Interoperability considerations:
      None, except as noted in other sections of this document.

   Published specification: [JS15]
   Applications which use this media type:
      Script interpreters as discussed in this document.

   Additional information:

      Magic number(s):             n/a
      File extension(s):           .js
      Macintosh File Type Code(s): TEXT

   Person & email address to contact for further information:
      See Author's Address section.

   Intended usage:          OBSOLETE
   Restrictions on usage:   n/a
   Author:                  See Author's Address section.
   Change controller:       The IESG.

7.2.  application/javascript

   Type name:               application
   Subtype name:            javascript
   Required parameters:     none
   Optional parameters:     charset, see section 4.1.
   Encoding considerations:
      The same as the considerations in section 3.2 of [RFC3023].

   Security considerations: See section 5.
   Interoperability considerations:
      None, except as noted in other sections of this document.

   Published specification: [JS15]
   Applications which use this media type:
      Script interpreters as discussed in this document.

   Additional information:

      Magic number(s):             n/a
      File extension(s):           .js
      Macintosh File Type Code(s): TEXT

   Person & email address to contact for further information:
      See Author's Address section.

   Intended usage:          COMMON
   Restrictions on usage:   n/a
   Author:                  See Author's Address section.
   Change controller:       The IESG.
1
  • 4
    you could have written what is said in the RFC
    – brunoais
    Commented Mar 7, 2012 at 17:22

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