174
<script type="text/javascript">
    /* ... */
</script>

vs.

<script language="Javascript">
    /* ... */
</script>

Which should be used and why?

Or, the third alternative: omitting either of these, such as the example code in jQuery's API reference:

<script src="http://code.jquery.com/jquery-latest.js"></script>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Ricket
  • 33,368
  • 30
  • 112
  • 143

3 Answers3

177

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.

gregers
  • 12,523
  • 8
  • 46
  • 41
Matchu
  • 83,922
  • 18
  • 153
  • 160
  • 11
    If you're using HTML5 and the script is JavaScript, specifying `type` is completely unnecessary as [text/javascript is the default](http://www.w3.org/TR/html5/scripting-1.html#the-script-element). – T.J. Crowder Nov 23 '14 at 18:26
  • Interestingly, you can use 'type' to set a different mime-type. For example type="text/plain" will allow you to hold unrendered text, which also not be run as a script. At least in Chromium-based browsers, so not sure about FF or Safari. Seemingly odd, but perfectly valid. I have found the useful in the past. A stated, if left out it is assumed to be 'text/javascript' by default in HTML5. – jdmayfield Apr 12 '23 at 00:55
41

HTML4/XHTML1 requires

<script type="...">...</script>

HTML5 faces the fact that there is only one scripting language on the web, and allows

<script>...</script>

The latter works in any browser that supports scripting (NN2+).

Ms2ger
  • 15,596
  • 6
  • 36
  • 35
  • 29
    HTML5 still supports 'Other scripting languages' on the web., but defaults the type to `text/javascript` if no type is explicitly defined. – Gordon Tucker Aug 03 '10 at 20:19
  • 7
    Sure. My point was that HTML4/XHTML1 didn't want to make JavaScript the default for reasons of theoretical purity, while HTML5 makes a saner trade-off. – Ms2ger Aug 04 '10 at 14:20
  • What are the other scripting languages for web? – Ivanka Todorova Apr 09 '13 at 22:36
  • 7
    @FakeHeal: VBScript saw some use on the client side when IE had massive domination. More recently, you can serve compile-to-JavaScript languages like CoffeeScript to the client with client-side compilation. Semi-standard WebGL practice is to put fragment shaders into script tags with a shader language for the type. Finally, there's some suspicion(?) that Dart might be natively supported in Chrome at some point. (I mean, of course, right?) – Simon Buchan Jun 03 '13 at 23:45
2

The type attribute is used to define the MIME type within the HTML document. Depending on what DOCTYPE you use, the type value is required in order to validate the HTML document.

The language attribute lets the browser know what language you are using (Javascript vs. VBScript) but is not necessarily essential and, IIRC, has been deprecated.

JasCav
  • 34,458
  • 20
  • 113
  • 170