为什么在服务器设置mime类型时写<script type ="text/javascript">?

Chr*_*man 64 html javascript css mime-types

我的理解是mime类型是由Web服务器设置的.为什么我们要添加type="text/javascripttype="text/css"属性?这不是一个无用且被忽略的属性吗?

bra*_*jam 66

道格拉斯·克罗克福德说:

type="text/javascript"

此属性是可选的.从Netscape 2开始,所有浏览器中的默认编程语言都是JavaScript.在XHTML中,此属性是必需且不必要的.在HTML中,最好不要使用它.浏览器知道该怎么做.

他还说:

W3C没有采用该language 属性,而是倾向于type 采用MIME类型的属性.不幸的是,MIME类型没有标准化,所以它有时 "text/javascript""application/ecmascript"其他.幸运的是,所有浏览器都将选择JavaScript作为默认编程语言,因此最好简单地编写<script>.它是最小的,适用于大多数浏览器.

仅出于娱乐目的,我尝试了以下五个脚本

  <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>
Run Code Online (Sandbox Code Playgroud)

在Chrome上,除了脚本3(type="baloney")之外的所有内容都有效.IE8没有运行脚本1(type="application/ecmascript")或脚本3.基于我对两个浏览器的非广泛样本,看起来你可以安全地忽略该type属性,但是你使用它,你最好使用合法的(浏览器相关的)值.

  • @Marcel:我在IE6中测试了它,只有`type = baloney`工作了:) (30认同)
  • @Daniel:很高兴知道.:)从现在开始,我将在我的页面上使用`<! - [if IE 6]> <script type = baloney>/*一些邪恶的崩溃代码*/</ script> <![endif] - >`.http://en.wiktionary.org/wiki/no_matter_how_thin_you_slice_it,_it%27s_still_baloney (18认同)
  • 这不符合HTML规范:http://www.w3.org/TR/html401/interact/scripts.html#adef-type-SCRIPT (3认同)

Mar*_*pel 28

因为,至少在HTML 4.01和XHTML 1(.1)中,元素的type属性<script>必需的.

HTML 5中,type不再需要.

实际上,虽然您应该text/javascript在HTML源代码中使用,但许多服务器将使用该文件发送文件Content-type: application/javascript.在RFC 4329中阅读有关这些MIME类型的更多信息.

请注意RFC 4329之间的差异,标记text/javascript为过时并建议使用application/javascript,以及某些浏览器对<script>包含的元素type="application/javascript"(在HTML源中,而不是发送的文件的HTTP Content-type头)感到惊讶的现实.最近,在WHATWG邮件列表上讨论了这种差异(HTML 5的type默认值text/javascript),请阅读这些主题的消息你会考虑RFC 4329吗?


Alo*_*hci 13

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.
   }
}
Run Code Online (Sandbox Code Playgroud)


Que*_*tin 5

它允许浏览器在发出脚本或样式表请求之前确定是否可以处理脚本/样式语言(或者,在嵌入式脚本/样式的情况下,确定正在使用哪种语言).

如果浏览器领域的语言之间存在更多的竞争,那将更加重要,但VBScript从未超越IE,PerlScript从未超越IE特定的插件,而JSSS开始时非常垃圾.

HTML5草案使该属性可选.