1

I know that doing

<script language="javascript" src="script.js">

or

<script src="script.js">

are the same. I just want to understand if there are cases when we must add language="javascript"?

3 Answers 3

5

The attribute is required if you are writing HTML 3.2 (which you should not be, this is the 21st century).

You may add it if you are writing HTML 4.x or XHTML 1.x (which you should not be, this is 2017).

It is obsolete (and should be omitted) if you are writing HTML 5.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, it was used when vbscript was a thing on IE. Now-a-days, we only use Javascript and the use of the attribute is nearly unnecessary. You could still use it to other languages. PHP<7 used <script language="php">...</script> as opening and closing tags.
1

From Obsolete but conforming features,

Authors should not specify a language attribute on a script element.

If the attribute is present, its value must be an ASCII case-insensitive match for the string "JavaScript" and either the type attribute must be omitted or its value must be an ASCII case-insensitive match for the string "text/javascript".

The attribute should be entirely omitted instead (with the value "JavaScript", it has no effect), or replaced with use of the type attribute.

So what's important is the type. You don't need to make it explicit, though.

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

Comments

0

According to MDN docs (a great reference for HTML, JS, DOM, etc.)

language
Like the type attribute, this attribute identifies the scripting language in use. Unlike the type attribute, however, this attribute’s possible values were never standardized. The type attribute should be used instead.

Therefore, the language attribute should probably not be used.

The type attribute in the same docs say:

type
This 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. If this attribute is absent, the script is treated as JavaScript.

Therefore, the minimal version is acceptable:

<script src="script.js">

And the most correct explicit version is:

<script type="text/javascript" src="script.js">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.