脚本标记的 type 属性的 javascript MIME 类型是什么?

Javascript 的 哑剧类型是什么?

更具体地说,在脚本标记的“ type”属性中放入什么是正确的事情?application/x-javascripttext/javascript似乎是主要的竞争者。

87364 次浏览

在一个脚本标签中,我会使用 text/javascript。

Http://www.w3.org/tr/rec-html40/interact/scripts.html

有趣的是,标准化应用程序/javascript 的 RFC已经有两年的历史了,但文本/javascript 仍然很常见。这又是一个习俗战胜标准的例子吗?它也出现在 HTML5

Text/javascript

我相信 IE 不接受 application/x-javascript

指定脚本语言

这是一个常见的错误。用于 javascript 的 MIME 类型多年来没有标准化。现在是 正式的: “ 应用程序/javascript”。

这里真正的问题是大多数浏览器不会使用这个属性,至少在 script 标记的情况下不会。它们实际上会窥视数据包内部,然后自己确定类型。

所以底线是 type = “ text/javascript”对于 javascript 来说没有任何作用,但是它是 HTML 4XHTML 1.0规范的一部分。

这是我找到的关于这个话题的第一页。

我的集体研究表明:

  1. 如果希望验证,则必须在 html4和 xhtml1中使用 Keparo 所说的 text/javascript。尽管浏览器忽略了它。
  2. 如果每个人都同意,当一切都赶上时,application/javascript预计将成为新的官方 MIME 类型。
  3. application/x-javascript(x 表示非正式的)是当前 javascript 的服务器端 MIME 引用。
  4. 每个人都希望,像往常一样,微软将决定做一些完全不同的事情,以进一步混淆和堵塞的问题。

总结: 现在,如果您希望您的 html/xhtml 在 MSIE 中工作并使用 W3C 进行验证,那么声明 type="text/javascript"。如果你想让你的网络服务器知道你的意思是 javascript 然后使用 application/x-javascript

定义 Javascript MIME 类型的官方 RFC 是 RFC4329

7.  JavaScript Media Types


7.1.  text/javascript (obsolete)


Type name:               text
Subtype name:            javascript
Required parameters:     none
Optional parameters:     charset, see section 4.1.
Encoding considerations:
The same as the considerations in section 3.1 of [RFC3023].


Security considerations: See section 5.
Interoperability considerations:
None, except as noted in other sections of this document.


Published specification: [JS15]
Applications which use this media type:
Script interpreters as discussed in this document.


Additional information:


Magic number(s):             n/a
File extension(s):           .js
Macintosh File Type Code(s): TEXT


Person & email address to contact for further information:
See Author's Address section.


Intended usage:          OBSOLETE
Restrictions on usage:   n/a
Author:                  See Author's Address section.
Change controller:       The IESG.


7.2.  application/javascript


Type name:               application
Subtype name:            javascript
Required parameters:     none
Optional parameters:     charset, see section 4.1.
Encoding considerations:
The same as the considerations in section 3.2 of [RFC3023].


Security considerations: See section 5.
Interoperability considerations:
None, except as noted in other sections of this document.


Published specification: [JS15]
Applications which use this media type:
Script interpreters as discussed in this document.


Additional information:


Magic number(s):             n/a
File extension(s):           .js
Macintosh File Type Code(s): TEXT


Person & email address to contact for further information:
See Author's Address section.


Intended usage:          COMMON
Restrictions on usage:   n/a
Author:                  See Author's Address section.
Change controller:       The IESG.