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

Html

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

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

Solution 3 - Html

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

Solution 4 - Html

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.

Solution 5 - Html

No, the 'type' attribute is not needed in

Solution 6 - Html

Solution 7 - Html

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

Source:https://www.w3schools.com/tags/tag_script.asp">W3Schools</a>,<a href="https://developer.mozilla.org/en-us/docs/Web/HTML/Element/script">MDN</a> > NOTE: if the type attribute is a audio, HTML, video, or any MIME type besides JavaScript the code will NOT be executed.

Solution 8 - Html

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYarinView Question on Stackoverflow
Solution 1 - HtmlEvan MulawskiView Answer on Stackoverflow
Solution 2 - HtmlalbuveeView Answer on Stackoverflow
Solution 3 - HtmlScott C WilsonView Answer on Stackoverflow
Solution 4 - HtmlJames ThompsonView Answer on Stackoverflow
Solution 5 - HtmlyazzerView Answer on Stackoverflow
Solution 6 - HtmltucazView Answer on Stackoverflow
Solution 7 - HtmlcoderView Answer on Stackoverflow
Solution 8 - HtmlBalaramakrishna RachumalluView Answer on Stackoverflow