为什么写< script type =" text / javascript">当mime类型由服务器设置时? [英] Why write <script type="text/javascript"> when the mime type is set by the server?
问题描述
我的理解是mime类型是由web服务器设置的。为什么我们添加 type =text / javascript
或 type =text / css
这是无用的和被忽略的属性?
My understanding is that mime types are set by the web server. Why do we add the type="text/javascript
or type="text/css"
attribute? Isn't this a useless and ignored attribute?
推荐答案
type =text / javascript
type="text/javascript"
此属性是可选的。从
Netscape 2开始,所有浏览器中的默认编程
语言都是
JavaScript。在XHTML中,这个属性
是必需的和不必要的。在HTML中,
最好离开它。
浏览器知道该做什么。
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.
He also says:
W3C未采用
语言
属性,而不是一个类型
属性,它接受一个MIME类型
不幸的是,MIME类型不是
标准化的,所以有时是
text / javascript
或
application / ecmascript
。幸运的是,所有浏览器都将
总是选择JavaScript作为
默认编程语言,所以它是
总是最好只写< script>
。
它是最小的,它适用于大多数浏览器的
。
W3C did not adopt the
language
attribute, favoring instead atype
attribute 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.
<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>
在Chrome上,除脚本3( type =baloney code>)工作。 IE8没有运行脚本1(
type =application / ecmascript
)或脚本3.基于我非常广泛的两个浏览器的示例, 类型
属性,但是它使用它你更好地使用法律(浏览器相关)的值。
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 type
attribute, but that it you use it you better use a legal (browser dependent) value.
这篇关于为什么写< script type =" text / javascript">当mime类型由服务器设置时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!