278

What is the difference between these headers?

Content-Type: application/javascript
Content-Type: application/x-javascript
Content-Type: text/javascript

Which one is best and why?

Please do not say they are identical - if they were identical there would not have been three of them. I know both work - but I would like to know the difference.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • 1
    The difference is also a classic reason why your scripts are not being compressed. Make sure you have an entry in httpCompression for the actual type you are serving and note that IIS Express only compresses application/x-javascript and text/* by default. – rism Feb 09 '15 at 01:38
  • NB: A full list of "javascript mime types" can be found here: https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type. i.e. this is the list of values which a browser should allow for a `script` tag's `type` attribute when the `nosniff` directive is specified. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options – JohnLBevan Oct 04 '17 at 12:40

6 Answers6

369

The JavaScript MIME Type

When sending JavaScript content, you should use text/javascript as per RFC 9239.

Aliases

application/javascript, application/x-javascript, text/javascript1.0, text/javascript1.1, text/javascript1.2, text/javascript1.3, text/javascript1.4, text/javascript1.5, text/jscript, and text/livescript are deprecated aliases for it. If you are writing a tool which consumes JavaScript (e.g. an HTTP client) then you should consider supporting them for backwards compatibility.

History

The text/javascript MIME type was used by convention until RFC 4329 attempted to replace it with application/javascript.

This was just a change so that the text/* and application/* MIME type groups had a consistent meaning where possible. (text/* MIME types are intended for human readable content, JavaScript is not designed to directly convey meaning to humans).

The industry largely ignored the specification so the current specification abandoned the attempt.

X- prefixes

Some of the MIME types mentioned here use an x- prefix. This was used to indicate experimental MIME types that had not been standardised. As per RFC 6648, this convention is deprecated.

HTML

While this question is about HTTP, it is worth mentioning the related type attribute in HTML.

When loading a traditional script, I recommend you omit the type attribute entirely. It has no effect but provides the opportunity to make a typo causing the browser to treat it as pointing to an unrecognised script type and ignore it. If you do provide it, then use text/javascript as some deprecated MIME types will not be recognised.

When loading a JavaScript module, use type="module" (note that this value is not a MIME type!).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thanks a lot for the detailed answer . one more issue - you have said that I can omit it entirely (only HTML5 ? ) - but my question (which was later edited by someone) was specifically about JS in PHP - will it work as PHP/JS combo on all servers/browsers if I will omit it entirely ?? – Obmerk Kronen Mar 12 '12 at 09:39
  • 10
    You can omit the `type` **attribute** on a ` – Quentin Mar 12 '12 at 09:47
22

mime-types starting with x- are not standardized. In case of javascript it's kind of outdated. Additional the second code snippet

<?Header('Content-Type: text/javascript');?>

requires short_open_tags to be enabled. you should avoid it.

<?php Header('Content-Type: text/javascript');?>

However, the completely correct mime-type for javascript is

application/javascript

http://www.iana.org/assignments/media-types/application/index.html

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    Old answer, but don't know if that's good to start with short open tags until that's not a best practice (we strongly recommand you to disable PHP-SOT in fact) – benftwc Sep 25 '17 at 16:13
8

As of May 2022, text/javascript is the preferred type once again (see RFC 9239)

The media type registrations herein are divided into two major categories: (1) the sole media type "text/javascript", which is now in common usage and (2) all of the media types that are obsolete

And

All registrations will point to this document as the reference. The outdated note stating that the "text/javascript" media type has been "OBSOLETED in favor of application/javascript" has been removed. The outdated note stating that the "text/ecmascript" media type has been "OBSOLETED in favor of application/ecmascript" has been removed. IANA has added the note "OBSOLETED in favor of text/javascript" to all registrations except "text/javascript"; that is, this note has been added to the "text/ecmascript", "application/javascript", and "application/ecmascript" registrations.

Machavity
  • 30,841
  • 27
  • 92
  • 100
itaton
  • 287
  • 2
  • 13
7

According to RFC 4329 the correct MIME type for JavaScript should be application/javascript. Howerver, older IE versions choke on this since they expect text/javascript.

Björn
  • 29,019
  • 9
  • 65
  • 81
  • 7
    As far as I'm aware, IE doesn't give a monkey's what the HTTP content type says; only what the HTML `type` attribute says (and in the HTML 5 drafts that attribute may be omitted for JavaScript). – Quentin Mar 12 '12 at 09:22
3

Use type="application/javascript"

In case of HTML5, the type attribute is obsolete, you may remove it. Note: that it defaults to "text/javascript" according to w3.org, so I would suggest to add the "application/javascript" instead of removing it.

http://www.w3.org/TR/html5/scripting-1.html#attr-script-type
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".

Use "application/javascript", because "text/javascript" is obsolete:

RFC 4329: http://www.rfc-editor.org/rfc/rfc4329.txt

  1. Deployed Scripting Media Types and Compatibility

    Various unregistered media types have been used in an ad-hoc fashion to label and exchange programs written in ECMAScript and JavaScript. These include:

    +-----------------------------------------------------+ | text/javascript | text/ecmascript | | text/javascript1.0 | text/javascript1.1 | | text/javascript1.2 | text/javascript1.3 | | text/javascript1.4 | text/javascript1.5 | | text/jscript | text/livescript | | text/x-javascript | text/x-ecmascript | | application/x-javascript | application/x-ecmascript | | application/javascript | application/ecmascript | +-----------------------------------------------------+

Use of the "text" top-level type for this kind of content is known to be problematic. This document thus defines text/javascript and text/
ecmascript but marks them as "obsolete". Use of experimental and
unregistered media types, as listed in part above, is discouraged.
The media types,

  * application/javascript
  * application/ecmascript

which are also defined in this document, are intended for common use and should be used instead.

This document defines equivalent processing requirements for the
types text/javascript, text/ecmascript, and application/javascript.
Use of and support for the media type application/ecmascript is
considerably less widespread than for other media types defined in
this document. Using that to its advantage, this document defines
stricter processing rules for this type to foster more interoperable
processing.

x-javascript is experimental, don't use it.

juFo
  • 17,849
  • 10
  • 105
  • 142
3

RFC 9239 Updates to ECMAScript Media Types

As mentioned by itaton, text/javascript is the preferred type once again

This RFC obsoletes RFC 4329 ("Scripting Media Types)", replacing the previous registrations with information and requirements aligned with common usage and implementation experiences.

The reason is due to not following BCP-13, see https://www.rfc-editor.org/info/bcp13

https://www.rfc-editor.org/rfc/rfc9239.html#section-6

The reason behind the change:

Finally, the [HTML] specification uses "text/javascript" as the default media type of ECMAScript when preparing script tags; therefore, "text/javascript" intended usage has been moved from OBSOLETE to COMMON. rfc9239

You can also test out the media types using this website: https://mathiasbynens.be/demo/javascript-mime-type

Sam Bacha
  • 81
  • 3