脚本标签中的" lang"和" type"属性有什么区别?

时间:2020-03-06 14:31:03  来源:igfitidea点击:

对于" <script>" HTML标记," lang = Javascript"和" type = text / javascript"之间的技术区别是什么?

我通常同时使用这两种方法,因为我一直认为较旧的浏览器需要其中一种。

解决方案

类型更为通用,是指脚本块的mime编码。据我所知,我们只需要一个,并且通常该块可以在没有type或者lag属性的情况下工作。

我倾向于使用类型。

语言是旧属性,类型是新属性。我们必须使用过渡(对此不是肯定,但可以肯定)文档类型来合法使用这两个属性。

lang是脚本的语言,而type是脚本标记内容的MIME类型。

&lt;script language ="">可用于提供VBScript和不同版本的Javascript。

除非我们需要特定版本的Javascript,否则不要使用language属性,否则,代码仍将正常运行。

即使部分代码确实需要特定的Javascript版本,也请尝试通过(typeof window.blah.feature!=" undefined")检查功能是否存在。

这是language属性用法的示例:
http://bclary.com/2004/08/27/javascript-version-incompatibilities

由于这种宽松定义的行为或者不确定的行为,不建议使用language属性。

type属性完全不同。它告诉浏览器脚本是什么mime类型,并且应该始终在script标记中指定。

根据HTML 4.01规范:

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.

基本上,这两个属性都不是必需的。使用它们的唯一原因是验证,这在HTML5中已经无效。