“<script type='text/javascript'>” 不正确吗?

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

is "<script type='text/javascript'>" incorrect?

javascripthtmlmime-types

提问by Raynos

On aminutewithbrendan, brendan eich makes an off hand comment implying that serving scripts as

在 aminutewithbrendan 上,brendan eich 发表了一个随意的评论,暗示将脚本作为

<script type='text/javascript'></script>

<script type='text/javascript'></script>

is not correct because "text/javascript"is not a valid MIME type and he states "application/javascript"is a valid MIME type.

不正确,因为"text/javascript"它不是有效的 MIME 类型,并且他声明"application/javascript"是有效的 MIME 类型。

I only care about serving HTML5 as the doctype.

我只关心将HTML5 作为 doctype提供服务。

  • Where are the MIME types for <script>defined in the html5 W3C specification ?
  • What is browser support like for "text/javascript"and "application/javascript"?
  • Which should be used ? Alternatively should we just not set typeat all?
  • <script>html5 W3C 规范中定义的 MIME 类型在哪里?
  • 是什么样的对浏览器的支持"text/javascript""application/javascript"
  • 应该使用哪个?或者,我们应该根本不设置type吗?

Literal Quote from brendan: (1:48)

来自布兰登的文字引述:(1:48)

... or script type equals application/javascript or application/ecmascript, those are the official MIME types or either one of those made-up ones from HTML4 like text/javascript ...

...或脚本类型等于 application/javascript 或 application/ecmascript,这些是官方 MIME 类型或 HTML4 中的任意一种,如 text/javascript ...

Related:

有关的:

The union of the related resources doesn't really answer all three questions.

相关资源的联合并没有真正回答所有三个问题。

回答by Quentin

Where are the MIME types for <script>defined in the html5 W3C specification ?

<script>html5 W3C 规范中定义的 MIME 类型在哪里?

Nowhere, it has a list(which includes some experimental and deprecated ones) but states that you can use any MIME type you like. MIME types are defined by IANAand text/javascriptis officially marked as obsoletein favour of application/javascript

无处可寻,它有一个列表(其中包括一些实验性和已弃用的列表),但声明您可以使用任何您喜欢的 MIME 类型。MIME 类型由 IANA 定义text/javascript并被正式标记为已过时以支持application/javascript

What is browser support like for "text/javascript" and "application/javascript" ?

什么是浏览器对“text/javascript”和“application/javascript”的支持?

Not good enough. There are still plenty of browsers around that don't recognise the latter. (This is, however, only a problem with the type attribute, you can set the HTTP Content-Type header correctly without worrying).

还不够好。仍然有很多浏览器无法识别后者。(然而,这只是 type 属性的问题,您可以正确设置 HTTP Content-Type 标头而无需担心)。

Which should be used ? Alternatively should we just not set type at all?

应该使用哪个?或者,我们应该根本不设置类型吗?

Since you only care about HTML 5, just omit the type attribute entirely. It is optional and the default language is JavaScript.

由于您只关心 HTML 5,因此只需完全省略 type 属性。它是可选的,默认语言是 JavaScript。

回答by james.garriss

I think Brandon is (at least) partially wrong. The latest editor's draft of HTML5 (15 Aug 2011) says:

我认为布兰登(至少)部分错误。HTML5 的最新编辑草案(2011 年 8 月 15 日)说:

The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".

type 属性给出了脚本的语言或数据的格式。如果该属性存在,则其值必须是有效的 MIME 类型。不得指定字符集参数。如果属性不存在,则使用的默认值是“text/javascript”。

So if you do not include a typeattribute, the default value is "text/javascript". If that is the default value, it must be a valid MIME type.

因此,如果您不包含type属性,则默认值为“text/javascript”。如果这是默认值,则它必须是有效的 MIME 类型。

What are the other valid MIME types? The spec doesn't seem to give an example list, but it does specify the required format when it says:

其他有效的 MIME 类型是什么?该规范似乎没有给出示例列表,但它确实指定了所需的格式:

A string is a valid MIME type if it matches the media-type rule defined in section 3.7 "Media Types" of RFC 2616

如果字符串与 RFC 2616 的第 3.7 节“媒体类型”中定义的媒体类型规则匹配,则该字符串是有效的 MIME 类型

Which you can have the joy of reading here:

您可以在这里享受阅读的乐趣:

http://www.ietf.org/rfc/rfc2616.txt

http://www.ietf.org/rfc/rfc2616.txt

Edit: Quentin is right: For HTML5, there's no need to include a typeattribute, assuming you're using Javascript.

编辑:Quentin 是对的:对于 HTML5 type,假设您使用的是 Javascript ,则无需包含属性。