20

For adding JavaScript to HTML, I have seen people use

<script language=javascript>

and

<script type="text/javascript">

It doesn’t seem like whether the script is embedded or external influences this decision.

Which one is preferred and why?

dakab
  • 5,379
  • 9
  • 43
  • 67
Foo
  • 4,206
  • 10
  • 39
  • 54
  • the attribute itself is optional, and really not necessary anymore. you can just use ` – PlantTheIdea Apr 12 '13 at 15:55
  • 1
    possible duplicate of [HTML Script tag: type or language (or omit both)?](http://stackoverflow.com/questions/2267476/html-script-tag-type-or-language-or-omit-both) – DaoWen Apr 12 '13 at 15:56
  • Does this answer your question? [HTML Script tag: type or language (or omit both)?](https://stackoverflow.com/questions/2267476/html-script-tag-type-or-language-or-omit-both) – RiggsFolly Jun 08 '23 at 13:02

2 Answers2

30

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

Stijn de Witt
  • 40,192
  • 13
  • 79
  • 80
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Yahoo uses a mix of – Foo Apr 12 '13 at 15:59
  • 2
    @Foo: You can still use deprecated features, and mix standards. *It's not good practice*, and isn't valid HTML, but most browsers will run it anyway. – gen_Eric Apr 12 '13 at 16:00
  • 1
    @RocketHazmat Without some solid source on the matter I would not trust every browser out there to refrain from trying to run a script block just because it has a weird type attribute set. And even if it does work all across the board I still think "horrendous" would be a better adjective than "neat" ;-) – aaaaaaaaaaaa Apr 12 '13 at 16:07
  • @eBusiness: Fair enough. I've just seen it done, and assumed it was fine. I can check to see what the spec says ^_^ – gen_Eric Apr 12 '13 at 16:12
  • 1
    Today, the recommended way to embed schema.org metadata in a web page is by embedding a JSON fragment in a script tag with `type="application/ld+json"`. So I guess it is save to say we can now trust browsers not to run it, as it is embedded this way on millions of sites. – Stijn de Witt Feb 12 '19 at 22:15
8

The language attribute is deprecated. Use type only. You don't need to specify type in HTML5, it's javascript per default.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • In HTML 5, you don't even need `type`! Just ` – gen_Eric Apr 12 '13 at 15:55
  • @AgustinMeriles that's valid in HMTL. Just not in XHTML. – Christoph Apr 12 '13 at 16:00
  • 1
    @Christoph - unless this person is using XML, i think they're safe. – PlantTheIdea Apr 12 '13 at 16:02
  • If they are using a browser, they are safe. Just stick your XHTML doctype on your horribly invalid HTML and it will still work more or less the same. Browsers might switch to some non-standards mode, but I think even that has been more or less phased out already. Browsers always only really used one parser/rendering engine anyway, this just supported some 'standards mode /quirks mode' toggle and that was it. XHTML just never really made it past the W3C validator. – Stijn de Witt Feb 12 '19 at 22:27