开发者

Is the 'type' attribute necessary for <script> tags?

I've seen both this:

<script src='http://ajax.google开发者_如何学运维apis.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?


For HTML 4.x, the type attribute is required. Source

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.


For HTML 5, it is optional. If it is not specified, it defaults to text/javascript. Source

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".

Recommendation: See HTML 5.2


For HTML 5.2, it should be omitted if using a valid JavaScript MIME type (e.g. text/javascript). Source

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.


in HTML5 you do not need the type attribute, but in HTML<=4 and (X)HTML it should be declared


The HTML 5.2 standard (late 2017) says you should omit the type attribute.

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.

(emphasis mine.)

Source: https://www.w3.org/TR/html52/semantics-scripting.html#element-attrdef-script-type


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.

Any browser that matters will parse it fine though.


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.

I created this little page for testing on github: http://kenshub.github.io/script-type.html


According to W3C it is required: http://www.w3.org/TR/REC-html40/interact/scripts.html


The type attribute is required in HTML4.01 but optional in HTML5. The type shows what type to be parsed.

Source:W3Schools,MDN

NOTE: if the type attribute is a audio, HTML, video, or any MIME type besides JavaScript the code will NOT be executed.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜