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"?
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.
<script language="php">...</script> as opening and closing tags.From Obsolete but conforming features,
Authors should not specify a
languageattribute on ascriptelement.If the attribute is present, its value must be an ASCII case-insensitive match for the string "
JavaScript" and either thetypeattribute 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 thetypeattribute.
So what's important is the type. You don't need to make it explicit, though.
The
typeattribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. Thecharsetparameter must not be specified.The default, which is used if the attribute is absent, is "
text/javascript".
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">