哪一个更好:< script type =" text / javascript"> ...< / script>或< script> ...< / script> [英] Which is better: <script type="text/javascript">...</script> or <script>...</script>

查看:95
本文介绍了哪一个更好:< script type =" text / javascript"> ...< / script>或< script> ...< / script>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用哪个更好或更方便:

 < script type =text / javascript> .. < /脚本> 

 <脚本> ...< /脚本> 


解决方案

您是否需要type属性?如果您使用HTML5,则不会。否则,是的。 HTML 4.01和XHTML 1.0根据需要指定了类型属性,而HTML5将其作为可选属性,默认为 text / javascript 。现在HTML5已被广泛实现,所以如果您使用HTML5文档类型,< script> ...< / script> 是有效的,并且是一个不错的选择。

至于type属性应该怎么做,2006年注册的MIME类型 application / javascript 替换 text / javascript 并且受所有主要浏览器(包括Internet Explorer 9)的当前版本支持。来自相关RFC 的引用:


因此,该文件定义了text / javascript和text / ecmascript,但将它们标记为过时。不鼓励使用上面部分列出的实验和未注册媒体类型。媒体类型,

  * application / javascript 
* application / ecmascript

在本文档中也有定义,它们用于常用,应该用来代替。


但是,包含版本8的IE不会在< script> 元素内执行< $ c>应用程序/ javascript 或应用程序/ ecmascript 的$ c> type 属性,所以如果你需要支持旧的IE,你坚持使用 text / javascript


Which is better or more convenient to use:

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

or

<script>...</script>

解决方案

Do you need a type attribute at all? If you're using HTML5, no. Otherwise, yes. HTML 4.01 and XHTML 1.0 specifies the type attribute as required while HTML5 has it as optional, defaulting to text/javascript. HTML5 is now widely implemented, so if you use the HTML5 doctype, <script>...</script> is valid and a good choice.

As to what should go in the type attribute, the MIME type application/javascript registered in 2006 is intended to replace text/javascript and is supported by current versions of all the major browsers (including Internet Explorer 9). A quote from the relevant RFC:

This document thus defines text/javascript and text/ecmascript but marks them as "obsolete". Use of experimental and unregistered media types, as listed in part above, is discouraged. The media types,

  * application/javascript
  * application/ecmascript

which are also defined in this document, are intended for common use and should be used instead.

However, IE up to and including version 8 doesn't execute script inside a <script> element with a type attribute of either application/javascript or application/ecmascript, so if you need to support old IE, you're stuck with text/javascript.

这篇关于哪一个更好:&lt; script type =&quot; text / javascript&quot;&gt; ...&lt; / script&gt;或&lt; script&gt; ...&lt; / script&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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