ChatGPT解决这个技术问题 Extra ChatGPT

Difference between application/x-javascript and text/javascript content types

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.

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.
NB: A full list of "javascript mime types" can be found here: html.spec.whatwg.org/multipage/…. 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. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

R
Ry-

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.


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 ??
You can omit the type attribute on a <script> element. You can't omit the Content-Type HTTP header … ever (if you don't specify it in PHP then PHP will default to text/html which is very wrong).
Following the human-readable logic, shouldn't CSS be classified under application as well instead of text?
@frnhr your edit to this answer changed the intended meaning (which was to state that text/javascript is obsolete and application/x-javascript was experimental). Worse, it left the start of the answer incoherent, with a block saying text/javascript just hanging out irrelevantly at the top of the answer for no obvious reason.
In SVN, definitely use text/javascript. SVN treats anything NOT starting with text/ as binary. To fix your entire SVN working copy, you need to create a mime.cmd file containing the following: @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.
K
KingCrunch

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


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)
B
Björn

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.


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 ahh... that's what was giving me trouble. Thanks!
j
juFo

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.