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.
script
tag's type
attribute when the nosniff
directive is specified. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…
text/javascript
is obsolete, and application/x-javascript
was experimental (hence the x-
prefix) for a transitional period until application/javascript
could be standardised.
You should use application/javascript
. This is documented in the RFC.
As far a browsers are concerned, there is no difference (at least in HTTP headers). 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).
Note that using application/javascript
in the type
attribute of a script element will cause the script to be ignored (as being in an unknown language) in some older browsers. Either continue to use text/javascript
there or omit the attribute entirely (which is permitted in HTML 5).
This isn't a problem in HTTP headers as browsers universally (as far as I'm aware) either ignore the HTTP content-type of scripts entirely, or are modern enough to recognise application/javascript
.
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
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
.
type
attribute says (and in the HTML 5 drafts that attribute may be omitted for JavaScript).
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 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.
Success story sharing
type
attribute on a<script>
element. You can't omit theContent-Type
HTTP header … ever (if you don't specify it in PHP then PHP will default totext/html
which is very wrong).text/javascript
is obsolete andapplication/x-javascript
was experimental). Worse, it left the start of the answer incoherent, with a block sayingtext/javascript
just hanging out irrelevantly at the top of the answer for no obvious reason.@echo off
for /r . %%X in (*.js) do (
svn propset svn:mime-type text/javascript "%%X"
)
which when executed, will change the mime type of all JS files in your repository to text/javascript. You then have to commit the JS files to SVN with the new mime type.