HTML脚本标记:类型或语言(或省略两者)? [英] HTML Script tag: type or language (or omit both)?

查看:141
本文介绍了HTML脚本标记:类型或语言(或省略两者)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script type =text / javascript> 
/ * ... * /
< / script>

vs。

 < script language =Javascript> 
/ * ... * /
< / script>

应该使用哪一个?为什么?

或者,第三个选择:省略其中的任何一个,例如jQuery API参考中的示例代码:

 < script src = http://code.jquery.com/jquery-latest.js >< /脚本> 


解决方案

语言属性已被弃用很长一段时间,并且不应该被使用。



当W3C使用HTML5时,他们发现所有浏览器都有text / javascript 作为默认脚本键入,所以他们将其标准化为默认值。因此,您不需要 type



对于XHTML 1.0或HTML 4.01中的页面,省略 type 被认为是无效的。尝试验证以下内容:

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ; 
< html xmlns =http://www.w3.org/1999/xhtmlxml:lang =enlang =en>
< head>
< script src =http://example.com/test.js>< / script>
< / head>
< body />
< / html>

您将收到以下错误消息:


第4行,第41列:required属性type未指定

重新成为标准的粉丝,使用它。它应该没有实际效果,但是,如果有疑问,不妨按规格行事。

<script type="text/javascript">
    /* ... */
</script>

vs.

<script language="Javascript">
    /* ... */
</script>

Which should be used and why?

Or, the third alternative: omitting either of these, such as the example code in jQuery's API reference:

<script src="http://code.jquery.com/jquery-latest.js"></script>

解决方案

The language attribute has been deprecated for a long time, and should not be used.

When W3C was working on HTML5, they discovered all browsers have "text/javascript" as the default script type, so they standardized it to be the default value. Hence, you don't need type either.

For pages in XHTML 1.0 or HTML 4.01 omitting type is considered invalid. Try validating the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://example.com/test.js"></script>
</head>
<body/>
</html>

You will be informed of the following error:

Line 4, Column 41: required attribute "type" not specified

So if you're a fan of standards, use it. It should have no practical effect, but, when in doubt, may as well go by the spec.

这篇关于HTML脚本标记:类型或语言(或省略两者)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆