哪一个更好:< script type =" text / javascript"> ...< / script&

编程入门 行业动态 更新时间:2024-10-27 06:18:55
本文介绍了哪一个更好:< 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> 元素内执行应用程序/ 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; ...&a

本文发布于:2023-09-18 08:36:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:script   type   amp   lt   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!