Do HTML5 Script Tag Need Type="Javascript"

Do HTML5 Script tag need type= javascript ?

No, it's now officially useless.

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

Simply do

<script src=yourpath.js></script>

(yes, you can omit the quotes too)

Note that you don't have to worry about pre-HTML5 browsers, all of them always considered JavaScript to be the default script language.

Do you need text/javascript specified in your script tags?

See Crockford's write-up on the <script> tag, most notably:

Do not use the <!-- //--> hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years. <!-- //--> is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include --, so a script that decrements has an HTML error.

...

type="text/javascript"

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.

Is the 'type' attribute necessary for script tags?

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.

Should I include type= text/javascript in my SCRIPT tags?

You misunderstood what Crockford meant, he didn't say the type attribute is completely invalid, it's just that it's incorrect. The MIME type for JavaScript is application/javascript (or application/ecmascript I can't remember right now).

The general usage though is that is text/javascript which browsers will handle without any problems as that's been the defacto standard for a long time.

In regards to the <script src="..." tag it is redundant because the server determines the MIME type of the file and that is what the browser will then deal with.

He best explains it in one of his videos on YUI Theater (http://developer.yahoo.com/yui/theater/). I don't remember exactly which one he talks about this, I think it was in the advanced JavaScript series (but yeah I've watched them all a few times so they kind of blur into each other).

So if you want to write valid XHTML you need to provide something like text/javascript but it's not the official MIME type of the JavaScript language.

Understanding the type attribute in a script tag

I want to understand the magic behind adding type='text/babel' to a script tag.

No real magic: The Babel script you include on the page looks for those elements and transpiles them into ES5 on-the-fly, then has the browser run the resulting ES5 code. Setting that type on the script elements does two things:

  1. Prevents the browser from trying to run them directly, and

  2. Identifies them for the Babel script.

Regarding type on script in general, from the specification:

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

Then later when explaining how to process script elements:

If the user agent does not support the scripting language given by the script block's type for this script element, then the user agent must abort these steps at this point. The script is not executed.


It's worth calling out what the Babel website says about transpiling in the browser:

Compiling in the browser has a fairly limited use case, so if you are working on a production site you should be precompiling your scripts server-side. See setup build systems for more information.

(Where they've said "compiling" most of us would say "transpiling.")

language= javascript vs. type= text/javascript

<script language="javascript"> was used in very old browsers, and is deprecated.

<script type="text/javascript"> is the HTML 4 standard.

In HTML 5, the type parameter is optional (text/javascript is the default), so you can just do <script>.

As a neat hack, if you put an invalid type, the script won't be ran, but you can still read the data in JavaScript. Some template libraries do this.

script tag vs script type = 'text/javascript' tag

In HTML 4, the type attribute is required. In my experience, all
browsers will default to text/javascript if it is absent, but that
behaviour is not defined anywhere. While you can in theory leave it
out and assume it will be interpreted as JavaScript, it's invalid
HTML, so why not add it.

In HTML 5, the type attribute is optional and defaults to
text/javascript

Use <script type="text/javascript"> or simply <script> (if omitted, the type is the same). Do not use <script language="JavaScript">; the language attribute is deprecated

Ref :

http://social.msdn.microsoft.com/Forums/vstudio/en-US/65aaf5f3-09db-4f7e-a32d-d53e9720ad4c/script-languagejavascript-or-script-typetextjavascript-?forum=netfxjscript

and

Difference between <script> tag with type and <script> without type?

Do you need type attribute at all?

I am using HTML5- No

I am not using HTML5 - Yes

HTML Script tag: type or language (or omit both)?

The language attribute has been deprecated for a long time, and should not be used.

When W3C was working on HTML5, they discovered all browsers have "text/javascript" as the default script type, so they standardized it to be the default value. Hence, you don't need type either.

For pages in XHTML 1.0 or HTML 4.01 omitting type is considered invalid. Try validating the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://example.com/test.js"></script>
</head>
<body/>
</html>

You will be informed of the following error:

Line 4, Column 41: required attribute "type" not specified

So if you're a fan of standards, use it. It should have no practical effect, but, when in doubt, may as well go by the spec.

Why write script type= text/javascript when the mime type is set by the server?

Douglas Crockford says:

type="text/javascript"

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 did not adopt the language
attribute, favoring instead a type
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.

For entertainment purposes only, I tried out the following five scripts

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

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.



Related Topics



Leave a reply



Submit