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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4243577/
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-23 11:33:24  来源:igfitidea点击:

Which is better: <script type="text/javascript">...</script> or <script>...</script>

javascripthtml

提问by pencilCake

Which is better or more convenient to use:

哪个更好或更方便使用:

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

or

或者

<script>...</script>

回答by Tim Down

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 typeattribute 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.

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

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

至于 type 属性中应该包含的内容application/javascript,2006 年注册的 MIME 类型旨在取代text/javascript所有主要浏览器(包括 Internet Explorer 9)的当前版本并支持。来自相关 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.

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

  * application/javascript
  * application/ecmascript

本文档中也定义了它们,旨在用于通用用途,应改为使用。

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

但是,IE 8 及以上版本不会在具有or属性的<script>元素内执行脚本,因此如果您需要支持旧版 IE,则只能使用.typeapplication/javascriptapplication/ecmascripttext/javascript

回答by Sarfraz

Both will work but xhtml standard requires you to specify the typetoo:

两者都可以工作,但 xhtml 标准要求您也指定type

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

<!ELEMENT SCRIPT - - %Script;          -- script statements -->
<!ATTLIST SCRIPT
  charset     %Charset;      #IMPLIED  -- char encoding of linked resource --
  type        %ContentType;  #REQUIRED -- content type of script language --
  src         %URI;          #IMPLIED  -- URI for an external script --
  defer       (defer)        #IMPLIED  -- UA may defer execution of script --
  >


type = content-type [CI] This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.

type = content-type [CI] 此属性指定元素内容的脚本语言并覆盖默认脚本语言。脚本语言被指定为内容类型(例如,“text/javascript”)。 作者必须为此属性提供一个值。此属性没有默认值。

Notices the emphasis above.

注意上面的强调。

http://www.w3.org/TR/html4/interact/scripts.html

http://www.w3.org/TR/html4/interact/scripts.html

Note:As of HTML5 (far away), the typeattribute is not required and is default.

注意:从 HTML5(远)开始,该type属性不是必需的并且是默认值。

回答by Sarfraz

You need to use <script type="text/javascript"> </script>unless you're using html5. In that case you are encouraged to prefer <script> ... </script>(because type attribute is specified by default to that value)

您需要使用<script type="text/javascript"> </script>,除非你使用HTML5。在这种情况下,我们鼓励您选择<script> ... </script>(因为 type 属性默认指定为该值)

回答by chovy

This is all that is needed:

这就是所需要的:

<!doctype html>
<script src="/path.js"></script>

回答by pooja

<script type="text/javascript"></script>because its the right way and compatible with all browsers

<script type="text/javascript"></script>因为它是正确的方式并且与所有浏览器兼容

回答by Stephen Elliott

With the latest Firefox, I must use:

使用最新的 Firefox,我必须使用:

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

Or else the script may not run properly.

否则脚本可能无法正常运行。