为什么要在服务器设置 MIME 类型时编写 <script type="text/javascript"> ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2706290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why write <script type="text/javascript"> when the mime type is set by the server?
提问by Christopher Altman
My understanding is that mime types are set by the web server. Why do we add the type="text/javascriptor type="text/css"attribute? Isn't this a useless and ignored attribute?
我的理解是 MIME 类型是由 Web 服务器设置的。为什么要添加type="text/javascriptortype="text/css"属性?这不是一个无用且被忽略的属性吗?
回答by brainjam
type="text/javascript"This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.
type="text/javascript"该属性是可选的。从 Netscape 2 开始,所有浏览器的默认编程语言都是 JavaScript。在 XHTML 中,此属性是必需的,也是不必要的。在 HTML 中,最好将其排除在外。浏览器知道该做什么。
W3C did not adopt the
languageattribute, favoring instead atypeattribute which takes a MIME type. Unfortunately, the MIME type was not standardized, so it is sometimes"text/javascript"or"application/ecmascript"or something else. Fortunately, all browsers will always choose JavaScript as the default programming language, so it is always best to simply write<script>. It is smallest, and it works on the most browsers.
W3C 没有采用该
language属性,而是倾向于type采用 MIME 类型的属性。不幸的是,MIME 类型没有标准化,所以它有时"text/javascript"或"application/ecmascript"或其他东西。幸运的是,所有浏览器都会选择 JavaScript 作为默认编程语言,因此最好只编写<script>. 它是最小的,适用于大多数浏览器。
For entertainment purposes only, I tried out the following five scripts
仅供娱乐,我尝试了以下五个脚本
<script type="application/ecmascript">alert("1");</script>
<script type="text/javascript">alert("2");</script>
<script type="baloney">alert("3");</script>
<script type="">alert("4");</script>
<script >alert("5");</script>
On Chrome, all but script 3 (type="baloney") worked. IE8 did not run script 1 (type="application/ecmascript") or script 3. Based on my non-extensive sample of two browsers, it looks like you can safely ignore the typeattribute, but that it you use it you better use a legal (browser dependent) value.
在 Chrome 上,除了脚本 3 ( type="baloney") 之外的所有内容都有效。IE8 没有运行脚本 1 ( type="application/ecmascript") 或脚本 3。基于我对两个浏览器的非广泛示例,看起来您可以安全地忽略该type属性,但如果您使用它,您最好使用合法的(依赖于浏览器的)值。
回答by Marcel Korpel
Because, at least in HTML 4.01 and XHTML 1(.1), the typeattribute for <script>elements is required.
因为,至少在 HTML 4.01 和 XHTML 1(.1) 中,元素的type属性<script>是必需的。
In HTML 5, typeis no longer required.
在HTML 5 中,type不再需要。
In fact, while you should use text/javascriptin your HTML source, many servers will send the file with Content-type: application/javascript. Read more about these MIME types in RFC 4329.
事实上,虽然您应该text/javascript在 HTML 源代码中使用,但许多服务器会发送带有Content-type: application/javascript. 在RFC 4329 中阅读有关这些 MIME 类型的更多信息。
Notice the difference between RFC 4329, that marked text/javascriptas obsolete and recommending the use of application/javascript, and the reality in which some browsers freak out on <script>elements containing type="application/javascript"(in HTML source, not the HTTP Content-type header of the file that gets send). Recently, there was a discussion on the WHATWG mailing list about this discrepancy (HTML 5's typedefaults to text/javascript), read these messages with subject Will you consider about RFC 4329?
请注意 RFC 4329(标记text/javascript为过时并推荐使用application/javascript)与某些浏览器对<script>包含的元素type="application/javascript"(在 HTML 源中,而不是发送的文件的 HTTP 内容类型标头)感到害怕的现实之间的区别。最近,在 WHATWG 邮件列表上有一个关于这个差异的讨论(HTML 5 的type默认为text/javascript),阅读这些主题为 RFC 4329 的消息你会考虑吗?
回答by Alohci
Boris Zbarsky (Mozilla), who probably knows more about the innards of Gecko than anyone else, provided at http://lists.w3.org/Archives/Public/public-html/2009Apr/0195.htmlthe pseudocode repeated below to describe what Gecko based browsers do:
Boris Zbarsky(Mozilla),他可能比其他任何人都更了解 Gecko 的内脏,在http://lists.w3.org/Archives/Public/public-html/2009Apr/0195.html提供了下面重复的伪代码来描述基于 Gecko 的浏览器做什么:
if (@type not set or empty) {
if (@language not set or empty) {
// Treat as default script language; what this is depends on the
// content-script-type HTTP header or equivalent META tag
} else {
if (@language is one of "javascript", "livescript", "mocha",
"javascript1.0", "javascript1.1",
"javascript1.2", "javascript1.3",
"javascript1.4", "javascript1.5",
"javascript1.6", "javascript1.7",
"javascript1.8") {
// Treat as javascript
} else {
// Treat as unknown script language; do not execute
}
}
} else {
if (@type is one of "text/javascript", "text/ecmascript",
"application/javascript",
"application/ecmascript",
"application/x-javascript") {
// Treat as javascript
} else {
// Treat as specified (e.g. if pyxpcom is installed and
// python script is allowed in this context and the type
// is one that the python runtime claims to handle, use that).
// If we don't have a runtime for this type, do not execute.
}
}
回答by Quentin
It allows browsers to determine if they can handle the scripting/style language before making a request for the script or stylesheet (or, in the case of embedded script/style, identify which language is being used).
它允许浏览器在请求脚本或样式表之前确定它们是否可以处理脚本/样式语言(或者,在嵌入式脚本/样式的情况下,确定正在使用哪种语言)。
This would be much more important if there had been more competition among languages in browser space, but VBScript never made it beyond IE and PerlScript never made it beyond an IE specific plugin while JSSS was pretty rubbish to begin with.
如果浏览器领域的语言之间存在更多竞争,这将变得更加重要,但 VBScript 从未超越 IE,而 PerlScript 从未超越 IE 特定插件,而 JSSS 一开始就相当垃圾。
The draft of HTML5 makes the attribute optional.
HTML5 草案使该属性可选。

