Coder Perfect

Is it necessary to include text/javascript in your script> tags?

Problem

I read somewhere that type=”text/javascript” and the strange CDATA and!— items in your script tags are no longer required. Therefore, instead of:

<script type="text/javascript">
//<![CDATA[
<!--

    //your script here

-->
//]]>
</script>

You’d simply do:

<script>
    //your script here
</script>

However, I can’t recall where I read this. I believe it was from a Google or Yahoo engineer, and they precisely said which browsers and why these outdated structures were required. Anyone know what blog post or article this was discussed in, or have a decent resource on the subject?

Asked by cmcculloh

Solution #1

See, for example, Crockford’s article on the script> tag:

This attribute is not required. JavaScript has been the default programming language in all browsers since Netscape 2. This attribute is both essential and superfluous in XHTML. It is preferable to leave it out in HTML. The browser is aware of the situation.

Answered by bdukes

Solution #2

It comes highly recommended by Crockford. I’m sure I’ve seen it elsewhere (ppk perhaps?). It is not required by the HTML5 specification.

Surprisingly, using the “type” attribute to designate script> blocks that you don’t want evaluated has become quite fashionable:

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

You can dump raw text into the page for usage by other JavaScript code by specifying a strange non-JavaScript type (which is presumably in script block that can be evaluated).

Answered by Pointy

Solution #3

The type=”text/javascript” attribute isn’t required in HTML5 (it’s the default).

CDATA is only required for XHTML pages that contain HTML characters (such as ” and ‘>’).

!— is only required for older browsers.

Answered by Rocket Hazmat

Solution #4

The type attribute identifies the scripting language of code placed within a script element or linked via the src attribute of the element. This is provided as a MIME type, with text/javascript, text/ecmascript, application/javascript, and application/ecmascript being examples of supported MIME types.

HTML 4.01 is a specification that specifies how to format a webpage.

However, because text/javascript is the default type in HTML5, you can skip it.

Answered by Mithun Sreedharan

Solution #5

I’m inclined to assert that no one uses text/javascript anymore, and that even minification tools would most likely delete it… In fact, the Facebook SDK documentation only mentions script>.

The Google SDK documentation, on the other hand, still includes text/javascript.

Text/javascript is still present in the Amazon SDK docs.

Text/javascript is still used in the LinkedIn API documentation.

Text/javascript is still used by Instagram.

Answered by Sergey Orshanskiy

Post is based on https://stackoverflow.com/questions/5265202/do-you-need-text-javascript-specified-in-your-script-tags