Html language="javascript" 与 type="text/javascript"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15975786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:29:47  来源:igfitidea点击:

language="javascript" vs. type="text/javascript"

javascripthtml

提问by Foo

For adding JavaScript to HTML, I have seen people use

为了将 JavaScript 添加到 HTML,我看到人们使用

<script language=javascript>

and

<script type="text/javascript">

It doesn't seem like whether the script is embedded or external influences this decision.

似乎脚本是嵌入的还是外部影响这个决定的。

Which one is preferred and why?

哪一个是首选,为什么?

回答by Rocket Hazmat

<script language="javascript">was used in veryold browsers, and is deprecated.

<script language="javascript">曾在非常旧的浏览器中使用,现已弃用。

<script type="text/javascript">is the HTML 4 standard.

<script type="text/javascript">是 HTML 4 标准。

In HTML 5, the typeparameter is optional (text/javascriptis the default), so you can just do <script>.

在 HTML 5 中,该type参数是可选的(text/javascript是默认值),因此您只需执行<script>.

As a neat hack, if you put an invalid type, the script won't be ran, but you can still read the data in JavaScript. Some template libraries do this.

作为一个巧妙的技巧,如果你输入一个 invalid type,脚本将不会运行,但你仍然可以在 JavaScript 中读取数据。一些模板库会这样做。

回答by Christoph

The languageattribute is deprecated. Use typeonly. You don't need to specify typein HTML5, it's javascript per default.

language属性已弃用type仅使用。您不需要type在 HTML5 中指定,默认情况下它是 javascript。