这些标头有什么区别?
Content-Type: application/javascript
Content-Type: application/x-javascript
Content-Type: text/javascript
哪一个是最好的,为什么?
请不要说它们是完全相同的 - 如果它们是完全相同的,就不会有三个了。我知道两个都能用 - 但我想知道它们之间的区别。
这些标头有什么区别?
Content-Type: application/javascript
Content-Type: application/x-javascript
Content-Type: text/javascript
哪一个是最好的,为什么?
请不要说它们是完全相同的 - 如果它们是完全相同的,就不会有三个了。我知道两个都能用 - 但我想知道它们之间的区别。
在发送JavaScript内容时,应按照RFC 9239使用text/javascript
。
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
和text/livescript
是其已弃用的别名。如果您正在编写一个使用 JavaScript 的工具(例如 HTTP 客户端),则应考虑支持它们以实现向后兼容性。
text/javascript
MIME类型一直是惯例,直到RFC 4329试图将其替换为application/javascript
。
text/*
和 application/*
MIME 类型组在可能的情况下有一致的含义而进行的更改。(text/*
MIME 类型旨在用于人类可读的内容,JavaScript 并不设计成直接传达给人类)。x-
前缀表示实验性 MIME 类型,尚未标准化。根据 RFC 6648,不建议使用此约定。
type
属性。
在加载传统脚本时,我建议您完全省略type
属性。它没有任何效果,但可能会因为打字错误而被浏览器视为指向无法识别的脚本类型并忽略它。如果您确实提供了它,请使用text/javascript
,因为一些已弃用的MIME类型将不被识别。
在加载JavaScript模块时,请使用type="module"
(请注意,此值不是MIME类型!)。
<script>
元素上,您可以省略type
属性。但是,在HTTP头中,您永远不能省略Content-Type
(如果您没有在PHP中指定它,则PHP将默认为text/html
,这是非常错误的)。 - Quentintext/javascript
已经过时,而 application/x-javascript
是试验性的)。更糟糕的是,它让答案的开头变得不连贯,一个块仅仅是说着 text/javascript
而没有明显的理由悬在答案的顶部。 - Mark Amery@echo off
for /r . %%X in (*.js) do (
svn propset svn:mime-type text/javascript "%%X"
)
执行此命令后,将会把代码库中所有 JS 文件的 MIME 类型更改为 text/javascript。然后,您需要使用新的 MIME 类型将 JS 文件提交到 SVN。 - Mark Eldridgex-
开头的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月,text/javascript
再次成为首选类型(请参见RFC 9239)。
此处的媒体类型注册分为两大类:(1)唯一的媒体类型“text/javascript”,现在已经普遍使用;(2)所有过时的媒体类型。
并且
所有注册都将指向本文档作为参考。已删除过时的注释,说明“text/javascript”媒体类型已被“application/javascript”取代。已删除过时的注释,说明“text/ecmascript”媒体类型已被“application/ecmascript”取代。IANA已将注释“已被text/javascript取代”添加到除“text/javascript”之外的所有注册中;也就是说,已将此注释添加到“text/ecmascript”、“application/javascript”和“application/ecmascript”注册中。
application/javascript
。然而,旧版本的IE会因为期望text/javascript
而出现问题。如itaton所述,text/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”的预期用途已从“OBSOLETE”移动到“COMMON”。rfc9239
您还可以使用此网站测试媒体类型:https://mathiasbynens.be/demo/javascript-mime-type
使用 type="application/javascript"
在HTML5中,type属性已过时,可以将其删除。请注意:根据w3.org的规定,默认为"text/javascript",因此建议添加"application/javascript",而不是删除它。
http://www.w3.org/TR/html5/scripting-1.html#attr-script-type
type属性指定了脚本的语言或数据格式。如果存在该属性,则其值必须是有效的MIME类型。不得指定charset参数。如果该属性不存在,则默认为"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是实验性的,请勿使用。
nosniff
指令时,浏览器应允许script
标签的type
属性使用的值列表。https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options - JohnLBevan