【发布时间】:2011-09-11 14:42:09
【问题描述】:
每当我们编写 javascript 时,我们都是这样声明的-
<Script type="text/javascript">
. . .
. . .
. . . </script>
但我不明白为什么它被声明为文本。
【问题讨论】:
标签: javascript mime-types
每当我们编写 javascript 时,我们都是这样声明的-
<Script type="text/javascript">
. . .
. . .
. . . </script>
但我不明白为什么它被声明为文本。
【问题讨论】:
标签: javascript mime-types
因为它是。 Javascript 源是文本。该谓词将确定数据的传输方式。然后,文本的类型(Javascript 源代码)决定了它的使用方式。
【讨论】:
【讨论】:
出于遗留原因。 Technically,我们应该使用application/javascript 或application/ecmascript,但实际上没有人这样做。
【讨论】:
因为它是纯文本而不是图像/或应用程序/但是它可能必须是应用程序,但没有人使用它,IE会失败
因为 text 是原来的样子,如果我们更改它,某些浏览器会失败,所以您应该保留 text/javascript。现在自从 HTML5 以来它被删除了,并且由于默认的 IS javascript,很多 SO 人主张简单地删除它。这可能会在某些 DOCTYPES 中产生验证错误
<script>
alert("UNLESS this is IE and there is a VBScript as the first script on the page,"+
"this WILL be JavaScript in all known browsers");
</script>
这也是不使用 onclick="javascript: something()" 的一个很好的理由,除非您使用某些 IDE 并希望快速找到您的内联处理程序
What is the javascript MIME type for the type attribute of a script tag?
【讨论】:
type 属性标识
<script>和之间的内容</script>标签。MIME 类型由两部分组成: 一种媒体类型和一种子类型。为了 JavaScript,MIME 类型是 “文本/javascript”。
尽管正如 Douglas Crockford 所说的 leave it out。
【讨论】:
text type 的选择可能没有任何具体原因:由于 JavaScript 是文本的,他们选择了 text。
但是对于这种内容使用 text 类型是有问题的,因为line breaks must be represented as a CRLF and vice versa 和default value of the charset parameter is assumed to be US-ASCII(另外还有HTTP specified ISO 8859-1 as default charset)。
这就是他们介绍application/javascript as the MIME type for JavaScript的原因。
【讨论】: