你需要在<脚本>中指定的文本/ JavaScript吗?标签? [英] Do you need text/javascript specified in your <script> tags?

查看:63
本文介绍了你需要在<脚本>中指定的文本/ JavaScript吗?标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某处读到你不再需要像 type =text / javascript和奇怪的 CDATA <! - 脚本标记中的内容。所以,而不是:

 < script type =text / javascript> 
//<![CDATA [
<! -

//您的脚本在这里

- >
//]]>
< / script>

您可以这样做:

 <脚本> 
//您的脚本在这里
< / script>

我不记得我在哪里阅读这篇文章。我认为它来自Google或Yahoo工程师,他们特别提到了哪些浏览器需要这些古老的构造以及为什么。任何人都知道博客文章/文章这是谈论,或有一个很好的资源谈论这个? 解决方案

克罗克福德在< script> 标签上写道,最显着的是:


不要使用<! - // - > hack与脚本。它旨在防止脚本在第一代浏览器Netscape 1和Mosaic上显示为文本。多年来它一直没有必要。 <! - // - > 应该表示HTML注释。评论应该被忽略,不被编译和执行。此外,HTML注释不包括 - ,所以递减的脚本有HTML错误。

...


type =text / javascript



该属性是可选的。由于Netscape 2是所有浏览器中的默认编程语言,都是JavaScript。在XHTML中,这个属性是必需的和不必要的。在HTML中,最好不要使用它。浏览器知道该怎么做。



I read somewhere that you no longer need things like type="text/javascript" and the weird CDATA and <!-- things in your script tags. So, instead of:

<script type="text/javascript">
//<![CDATA[
<!--

    //your script here

-->
//]]>
</script>

You would just do:

<script>
    //your script here
</script>

I can't remember where I read this though. It was from a Google or Yahoo engineer I think, and they specifically mentioned which browsers required these archaic constructs and why. Anyone know what blog post/article this was talked about, or have a good resource talking about this?

解决方案

See Crockford's write-up on the <script> tag, most notably:

Do not use the <!-- //--> hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years. <!-- //--> is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include --, so a script that decrements has an HTML error.

...

type="text/javascript"

This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.

这篇关于你需要在&lt;脚本&gt;中指定的文本/ JavaScript吗?标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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