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

JavascriptHtml

Javascript Problem Overview


I read somewhere that you no longer need things like type="text/javascript" and the weird CDATA and <!-- things in your script tags. So, instead of:

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

    //your script here

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

You would just do:

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

I can't remember where I read this though. It was from a Google or Yahoo engineer I think, and they specifically mentioned which browsers required these archaic constructs and why. Anyone know what blog post/article this was talked about, or have a good resource talking about this?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

you may be thinking of this article here with the dependency being that scripts default to text/javascript in HTML5 automatically, while non-HTML5 browsers still expect that you define the type specifically spec-wise even though they will almost always guess text/javascript anyways.

Solution 7 - Javascript

it's up to the browser to interpret the script block correctly based on the headers, i believe, and not the type attribute. So to answer your question, no it is not required for modern browsers (i'm talking IE7+, FF, Webkit). If you are supporting older browsers than that...I feel sorry for you =)

Solution 8 - Javascript

If you're putting a script tag inside SVG you must specify the type attribute. And it should be "text/ecmascript" rather than "text/javascript".

If your script is inline (not linked) you will need to wrap the script body in a CDATA declaration too. The inline script boilerplate for SVG (and other XML variants) is thus

<script type="text/ecmascript">
<![CDATA[
// your javascript code goes here
]]>
</script>

These might be special cases 'in the wild', but they are real enough, and SVG use is growing, so it's incorrect for anyone else to suggest that the type attribute and CDATA are entirely obsolete in modern browsers. The use cases are narrow, yes, but not unheard of.

> "Change the environment to its opposite and every piece of wisdom becomes the worst of folly." - Ashby

Solution 9 - Javascript

>  The HTML5 specification urges authors to omit the attribute rather than provide a redundant MIME type. MDN

The MIME Sniffing Standard lets JavaScript be served using any MIME type (Multipurpose Internet Mail Extensions) that matches the following:

<script type="application/javascript"></script>
<script type="application/ecmascript"></script>

<script type="text/javascript"></script>
<script type="text/ecmascript"></script>

Solution 10 - Javascript

Well i keep seeing more examples without the text/javascript but for some reason my scripts wont work in FF when i do so. I would recommend keeping the text/javascript declaration. The CDATA tag prevents javascript from being shown as plain text in your website if your browser has javascript turned off. Personally i don't use those tags anymore don't think there's allot of users out there without and if they are out there they might wanna grow some brains :P

Solution 11 - Javascript

type="text/javascript" : Required in HTML 4 and XHTML, but optional in HTML5.

CDATA : Required in XHTML.

<!-- : Used to hide the JavaScript from very old browsers. Eg: Netscape 1 and Internet Explorer 2, neither of which anyone uses any more.

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
QuestioncmccullohView Question on Stackoverflow
Solution 1 - JavascriptbdukesView Answer on Stackoverflow
Solution 2 - JavascriptPointyView Answer on Stackoverflow
Solution 3 - JavascriptRocket HazmatView Answer on Stackoverflow
Solution 4 - JavascriptMithun SreedharanView Answer on Stackoverflow
Solution 5 - JavascriptSergey OrshanskiyView Answer on Stackoverflow
Solution 6 - JavascriptFatherStormView Answer on Stackoverflow
Solution 7 - JavascripthellatanView Answer on Stackoverflow
Solution 8 - JavascriptbrennanyoungView Answer on Stackoverflow
Solution 9 - JavascriptAndy YoungView Answer on Stackoverflow
Solution 10 - JavascriptMichaelView Answer on Stackoverflow
Solution 11 - JavascriptBlesson JoseView Answer on Stackoverflow