admin管理员组文章数量:1129704
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.
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.
Share Improve this question edited Jan 16, 2019 at 23:33 mikemaccana 123k110 gold badges425 silver badges529 bronze badges asked Mar 12, 2012 at 9:12 Obmerk KronenObmerk Kronen 15.9k17 gold badges69 silver badges106 bronze badges 2 |6 Answers
Reset to default 385The 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!).
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
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.
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
.
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
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.
本文标签: http headersDifference between applicationxjavascript and textjavascript content typesStack Overflow
版权声明:本文标题:http headers - Difference between applicationx-javascript and textjavascript content types - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736748531a1950911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
script
tag'stype
attribute when thenosniff
directive is specified. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/… – JohnLBevan Commented Oct 4, 2017 at 12:40