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.


It's a Crockford recommendation. I know I've seen it echoed elsewhere (ppk maybe?). The HTML5 spec does not require it.

Oddly, it's become somewhat au courant to use the "type" attribute to mark <script> blocks that you don't want to be evaluated:

<script type='text/html-template'>
  <div> this is a template </div>
</script>

By giving a weird non-JavaScript type, you get a way to stuff raw text into the page for use by other JavaScript code (which is presumably in script block that can be evaluated).


HTML5 doesn't need the type="text/javascript" (it's the default).

CDATA is only neeed for XHTML pages, if the script has any HTML characters (like '<' and '>') in it.

<!-- should only be needed for OLD browsers.


Well, I am tempted to say that nobody is using text/javascript any more, and that even minification tools would probably remove it... Indeed, Facebook SDK documentation specifies just <script>.

However, Google SDK documentation still has text/javascript.

Amazon SDK documentation still has text/javascript.

Linkedin API documentation still has text/javascript.

Instagram is still using text/javascript.


The type attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript.

According to HTML 4.01 Specification

The type 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.

But in HTML5 text/javascript is the default type, so you can omit

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