Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

People also ask

Do you need type attribute in script tag?

The type attribute is required in HTML4. 01 but optional in HTML5. The type shows what type to be parsed. NOTE: if the type attribute is a audio, HTML, video, or any MIME type besides JavaScript the code will NOT be executed.

What is type attribute in script tag?

The type attribute specifies the type of the script. The type attribute identifies the content between the <script> and </script> tags.

What are the two attributes of script tag?

Attributes: Many attribute associated with script tag. async: It is used to specify the script is executed asynchronously. charset: It is used to specify the character encoding used in an external script file.


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.