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

Content-Type: application/javascript
Content-Type: application/x-javascript
Content-Type: text/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

其他回答

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

rfc9239更新ECMAScript媒体类型

正如itaton提到的,文本/javascript再次成为首选类型

这个RFC取代了RFC 4329(“脚本媒体类型”),用与常用用法和实现经验相一致的信息和需求取代了以前的注册。

原因是没有遵循BCP-13,详见https://www.rfc-editor.org/info/bcp13

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

改变背后的原因:

最后,[HTML]规范在准备脚本标签时使用“text/javascript”作为ECMAScript的默认媒体类型;因此,“text/javascript”的预期用法已经从过时转移到通用。rfc9239

你也可以使用这个网站来测试媒体类型:https://mathiasbynens.be/demo/javascript-mime-type

使用类型=“应用程序/ 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是实验性的,不要使用它。

以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

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