What is the difference between “lang” and “type” attributes in a script tag?

StackOverflow https://stackoverflow.com/questions/112482

  •  02-07-2019
  •  | 
  •  

Question

For <script> HTML tags, what is the technical difference between lang=Javascript and type=text/javascript?

I usually use both, because I've always assumed that older browsers need one or the other.

Was it helpful?

Solution

Per the HTML 4.01 Spec:

type: This 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.

language: Deprecated. This attribute specifies the scripting language of the contents of this element. Its value is an identifier for the language, but since these identifiers are not standard, this attribute has been deprecated in favor of type.

OTHER TIPS

<script language=""> can be used for serving VBScript and different versions of Javascript.

Unless you need a specific version of Javascript, don't use the language attribute, your code will still work as normal without it.

Even if you do need a specific Javascript version for some part of the code, try to test if the feature exists instead, with a (typeof window.blah.feature != "undefined") check.

Here is an example of the language attribute's usage: http://bclary.com/2004/08/27/javascript-version-incompatibilities

The language attribute is deprecated because of this loosely defined or uncertain behaviour.

The type attribute is different entirely. It tells the browser what mime type the script is, and should always be specified in a script tag.

language is the old attribute, type is the new one. You'd have to use a transitional (not positive on that, but fairly sure) doctype to legally use both attributes.

The OP specifically said "lang" not "language". The much older "language" tag would have been Javascript or VBScript.

But the current and seemingly valid "lang" tag is actually which written language like English, Spanish, Japanese. Microsoft's Visual Studio provides a dropdown list for the values for "lang" and they are all like en-us, fr, ja, etc.. for English US, French, Japanese, etc...

I'm thinking there could be valid reasons for using this tag if you have a complex multilingual setup - maybe there's a content mgmt system that could support this and then deliver the proper javascript file - like jQuery control resources?

Type is more general and refers to the mime encoding of the script block. As far as I know you only need one and usually the block will work without either type or lag attributes.

I tend to use type.

lang is the language of the script, and type is the MIME type of the content of the script tag.

Basically, neither attribute is necessary. The only reason to use them is validation, and this has become void in HTML5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top