Ala Eddine JEBALI
Ala Eddine JEBALI

Reputation: 7891

What is the reason to use the attribute 'script language="javascript"' when inserting a JavaScript file?

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

Upvotes: 1

Views: 111

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241728

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

Upvotes: 0

Oriol
Oriol

Reputation: 288520

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

Upvotes: 1

Quentin
Quentin

Reputation: 944083

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.

Upvotes: 5

Related Questions