Html <script> 标签是否需要“type”属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4195427/
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
Is the 'type' attribute necessary for <script> tags?
提问by Yarin
I've seen both this:
这两个我都见过:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
and this:
和这个:
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
Does the type attribute matter in any way?
类型属性有任何影响吗?
采纳答案by Evan Mulawski
For HTML 4.x, the type
attribute is required. Source
对于HTML 4.x,该type
属性是必需的。来源
This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.
此属性指定元素内容的脚本语言并覆盖默认脚本语言。脚本语言被指定为内容类型(例如,“text/javascript”)。作者必须为此属性提供一个值。此属性没有默认值。
For HTML 5, it is optional. If it is not specified, it defaults to text/javascript
. Source
对于HTML 5,它是可选的。如果未指定,则默认为text/javascript
。来源
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".
type 属性给出了脚本的语言或数据的格式。如果该属性存在,则其值必须是有效的 MIME 类型。不得指定字符集参数。如果属性不存在,则使用的默认值是“text/javascript”。
Recommendation: See HTML 5.2
建议:参见HTML 5.2
For HTML 5.2, it should be omitted if using a valid JavaScript MIME type(e.g. text/javascript
). Source
对于HTML 5.2,如果使用有效的 JavaScript MIME 类型(例如text/javascript
),则应将其省略。来源
Omitting the attribute, or setting it to a JavaScript MIME type, means that the script is a classic script, to be interpreted according to the JavaScript Script top-level production. Classic scripts are affected by the charset, async, and defer attributes. Authors should omit the attribute, instead of redundantly giving a JavaScript MIME type.
省略该属性,或将其设置为 JavaScript MIME 类型,意味着该脚本是一个经典脚本,将根据 JavaScript Script 顶级产品进行解释。经典脚本受 charset、async 和 defer 属性的影响。作者应该省略该属性,而不是多余地给出 JavaScript MIME 类型。
回答by albuvee
in HTML5 you do not need the type
attribute, but in HTML<=4 and (X)HTML it should be declared
在 HTML5 中不需要该type
属性,但在 HTML<=4 和 (X)HTML 中应该声明它
回答by Scott C Wilson
The HTML 5.2 standard (late 2017) says you should omit the type attribute.
HTML 5.2 标准(2017 年末)说你应该省略 type 属性。
The type attribute allows customization of the type of script represented:
Omitting the attribute, or setting it to a JavaScript MIME type, means that the script is a classic script, to be interpreted according to the JavaScript Script top-level production. Classic scripts are affected by the charset, async, and defer attributes. Authors should omit the attribute, instead of redundantly giving a JavaScript MIME type.
type 属性允许自定义表示的脚本类型:
省略该属性,或将其设置为 JavaScript MIME 类型,意味着该脚本是一个经典脚本,将根据 JavaScript Script 顶级产品进行解释。经典脚本受 charset、async 和 defer 属性的影响。作者应该省略 属性,而不是多余地给出 JavaScript MIME 类型。
(emphasis mine.)
(强调我的。)
Source: https://www.w3.org/TR/html52/semantics-scripting.html#element-attrdef-script-type
来源:https: //www.w3.org/TR/html52/semantics-scripting.html#element-attrdef-script-type
回答by James Thompson
For testing purposes it WILL work without effect, but W3C requires it to validate correctly. The HTML 5 draft doesn't call for it as a requirement, but that is still in draft form at the W3C and they aren't recommending it.
出于测试目的,它将无效果地工作,但 W3C 要求它正确验证。HTML 5 草案并未要求将其作为要求,但 W3C 仍处于草案形式,他们不建议这样做。
Any browser that matters will parse it fine though.
任何重要的浏览器都会很好地解析它。
回答by yazzer
No, the 'type' attribute is not needed in tags. Unless you want your code to validate.
不,标签中不需要“类型”属性。除非你想让你的代码验证。
We've been omitting it for years at work, and it's never been an issue. We test heavily across multiple devices, browsers, etc. This includes various HTML4 & XHTML doctypes.
多年来,我们一直在工作中忽略它,这从来都不是问题。我们在多种设备、浏览器等上进行了大量测试。这包括各种 HTML4 和 XHTML 文档类型。
I created this little page for testing on github: http://kenshub.github.io/script-type.html
我创建了这个小页面用于在 github 上进行测试:http: //kenshub.github.io/script-type.html
回答by tucaz
According to W3C it is required: http://www.w3.org/TR/REC-html40/interact/scripts.html
根据 W3C 要求:http: //www.w3.org/TR/REC-html40/interact/scripts.html
回答by coder
The type attribute is required in HTML4.01 but optional in HTML5. The type shows what type to be parsed.
type 属性在 HTML4.01 中是必需的,但在 HTML5 中是可选的。类型显示要解析的类型。
NOTE: if the type attribute is a audio, HTML, video, or any MIME type besides JavaScript the code will NOT be executed.
注意:如果 type 属性是音频、HTML、视频或除 JavaScript 之外的任何 MIME 类型,则不会执行代码。
回答by Balaramakrishna Rachumallu
Though type attribute is not necessary it is helpful in most of the cases. IE works fine with out that but some times chrome and FF will cause an issue especially if the script is large. I faced issue with type attribute while working on plotly js library. With out the type attribute IE is able to show the graphs but FF, Chrome and edge. After having added type attribute chrome and FF are able to show the charts/graphs
尽管 type 属性不是必需的,但在大多数情况下它是有帮助的。IE 可以正常工作,但有时 chrome 和 FF 会导致问题,尤其是在脚本很大的情况下。我在处理 plotly js 库时遇到了 type 属性的问题。没有类型属性 IE 能够显示图形,但 FF、Chrome 和边缘。添加类型属性后 chrome 和 FF 能够显示图表/图形