这些头文件之间有什么区别?

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

哪个是最好的,为什么?

请不要说他们是一模一样的——如果他们是一模一样的,就不会有三个了。我知道这两种方法都有效,但我想知道其中的区别。


当前回答

使用类型=“应用程序/ javascript”

在HTML5中,type属性是过时的,你可以删除它。注意:根据w3.org,它默认为“文本/javascript”,所以我建议添加“应用程序/javascript”而不是删除它。

http://www.w3.org/TR/html5/scripting-1.html#attr-script-type 类型 属性提供脚本的语言或数据的格式。如果 属性存在时,其值必须是有效的MIME类型。的 不能指定字符集参数。默认值,如果 属性不存在,是"text/javascript"。

使用"application/javascript",因为"text/javascript"已经过时了:

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是实验性的,不要使用它。

其他回答

使用类型=“应用程序/ javascript”

在HTML5中,type属性是过时的,你可以删除它。注意:根据w3.org,它默认为“文本/javascript”,所以我建议添加“应用程序/javascript”而不是删除它。

http://www.w3.org/TR/html5/scripting-1.html#attr-script-type 类型 属性提供脚本的语言或数据的格式。如果 属性存在时,其值必须是有效的MIME类型。的 不能指定字符集参数。默认值,如果 属性不存在,是"text/javascript"。

使用"application/javascript",因为"text/javascript"已经过时了:

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是实验性的,不要使用它。

根据RFC 4329, JavaScript的正确MIME类型应该是application/ JavaScript。然而,旧的IE版本在这方面受阻,因为他们期望文本/javascript。

截至2022年5月,文本/javascript再次成为首选类型(参见RFC 9239)

Text /javascript已经过时了,application/x-javascript只是一个试验性的(因此前缀是x),直到application/javascript被标准化。

你应该使用application/javascript。RFC中记录了这一点。

就浏览器而言,没有区别(至少在HTTP报头上)。这只是一个更改,以便text/*和application/* MIME类型组在可能的情况下具有一致的含义。(text/* MIME类型是为人类可读的内容而设计的,JavaScript不是为直接向人类传达含义而设计的)。

注意,在一些旧的浏览器中,在脚本元素的type属性中使用application/javascript会导致脚本被忽略(因为是未知语言)。要么继续使用text/javascript,要么完全省略该属性(这在HTML 5中是允许的)。

这在HTTP报头中不是问题,因为浏览器普遍(据我所知)要么完全忽略HTTP内容类型的脚本,要么足够现代以识别应用程序/javascript。

以x-开头的mime类型不是标准化的。对于javascript来说,它有点过时了。 附加第二个代码片段

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

需要启用short_open_tags。你应该避免这样做。

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

然而,javascript完全正确的mime类型是

application/javascript

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