你真的需要指定type属性吗? [重复]

时间:2021-06-01 21:23:27

Possible Duplicate:
Why write <script type=“text/javascript”> when the mime type is set by the server?

可能重复:为什么在服务器设置mime类型时写

I read Dive into HTML5 a while back, and read its semantics chapter again just recently. I noted it advises not to use type="..." attributes on script and style, because:

我一会儿读了Dive到HTML5,最近又读了它的语义章节。我注意到它建议不要在脚本和样式上使用type =“...”属性,因为:

  • The MIME type should be sent by the server,
  • MIME类型应由服务器发送,
  • JS and CSS are the defaults,
  • JS和CSS是默认值,
  • Browsers don't care.
  • 浏览器不关心。

However, I see it is still common practice to include type attributes (or, horror, language) on both script and style tags. Assuming the server is properly configured to send the correct MIME types, are there reasons for using these other than being explicit?

但是,我发现在脚本和样式标记上包含类型属性(或恐怖,语言)仍然是常见做法。假设服务器已正确配置为发送正确的MIME类型,是否有理由使用这些而非显式?

EDIT: This is explicitly about HTML5, not XHTML.

编辑:这是明确的HTML5,而不是XHTML。

6 个解决方案

#1


21  

Most people are used to HTML 4/XHTML and before, where the type attribute is required for these elements.

大多数人习惯于使用HTML 4 / XHTML,之前需要这些元素的type属性。

In regards to HTML 5, these are indeed optional and the spec gives a default, depending on the element.

关于HTML 5,这些确实是可选的,并且规范给出了默认值,具体取决于元素。

For the script tag, this defaults to text/javascript:

对于脚本标记,默认为text / javascript:

If the language is not that described by "text/javascript", then the type attribute must be present

如果语言不是“text / javascript”描述的语言,则必须存在type属性

For the style tag, this defaults to text/css:

对于样式标记,默认为text / css:

The default value for the type attribute, which is used if the attribute is absent, is "text/css".

如果属性不存在,则使用type属性的默认值为“text / css”。

So, not needed, as you stated. However, browser support and server setups can't always be relied on - being explicit is a good idea as it avoids such problems.

所以,不需要,如你所说。但是,不能总是依赖浏览器支持和服务器设置 - 明确是一个好主意,因为它避免了这样的问题。

And of course, not all browsers out there support HTML 5 - those that don't will use an earlier version where the attribute is required and your javascript/css might not get parsed in such browsers, meaning you end up with no CSS or javascript on older browsers, when a simple solution for backwards compatibility is to add the attribute.

当然,并非所有浏览器都支持HTML 5 - 那些不会使用早期版本的属性是必需的,你的javascript / css可能无法在这样的浏览器中解析,这意味着你最终没有CSS或javascript在较旧的浏览器上,当向后兼容性的简单解决方案是添加属性时。

#2


4  

The type attribute may not be required for HTML5 but it is required for other HTML Doc Types such as HTML 4.01 Strict. I'd also say that anything making the code/document clearer for the developer is really only ever a good thing.

HTML5可能不需要type属性,但HTML 4.01 Strict等其他HTML Doc Types需要它。我还说,任何能让开发人员更清楚代码/文档的东西都是一件好事。

If that means being explicit about the type of script being used or the type of style, I'd use it.

如果这意味着要明确使用的脚本类型或样式类型,我会使用它。

#3


1  

That's not good. In XHTML, the type attribute is strictly required. Although browsers may be lenient, that's no reason to break convention.

这不好。在XHTML中,严格要求type属性。虽然浏览器可能很宽松,但没有理由违反惯例。

#4


1  

The type attribute is indeed not required for HTML5, but including it doesn't break validation, so you can convert to HTML5 from either HTML 4 or XHTML 1, and still have your <script> and <style> tags validate.

HTML5确实不需要type属性,但包括它不会破坏验证,因此您可以从HTML 4或XHTML 1转换为HTML5,并且仍然可以验证

<link> tags also do not need a type attribute (emphasis added):

标签也不需要类型属性(强调添加):

The type attribute gives the MIME type of the linked resource. It is purely advisory. The value must be a valid MIME type.

type属性提供链接资源的MIME类型。这纯粹是建议性的。该值必须是有效的MIME类型。

For external resource links, the type attribute is used as a hint to user agents so that they can avoid fetching resources they do not support. If the attribute is present, then the user agent must assume that the resource is of the given type (even if that is not a valid MIME type, e.g. the empty string). If the attribute is omitted, but the external resource link type has a default type defined, then the user agent must assume that the resource is of that type. If the UA does not support the given MIME type for the given link relationship, then the UA should not obtain the resource; if the UA does support the given MIME type for the given link relationship, then the UA should obtain the resource at the appropriate time as specified for the external resource link's particular type. If the attribute is omitted, and the external resource link type does not have a default type defined, but the user agent would obtain the resource if the type was known and supported, then the user agent should obtain the resource under the assumption that it will be supported.

对于外部资源链接,type属性用作用户代理的提示,以便它们可以避免获取它们不支持的资源。如果该属性存在,则用户代理必须假定该资源属于给定类型(即使该资源不是有效的MIME类型,例如空字符串)。如果省略该属性,但外部资源链接类型已定义默认类型,则用户代理必须假定该资源属于该类型。如果UA不支持给定链接关系的给定MIME类型,则UA不应获取该资源;如果UA确实支持给定链接关系的给定MIME类型,则UA应在为外部资源链接的特定类型指定的适当时间获取资源。如果省略该属性,并且外部资源链接类型没有定义默认类型,但是如果类型已知且受支持,则用户代理将获取资源,那么用户代理应该假设它将获得资源得到支持。

User agents must not consider the type attribute authoritative — upon fetching the resource, user agents must not use the type attribute to determine its actual type. Only the actual type (as defined in the next paragraph) is used to determine whether to apply the resource, not the aforementioned assumed type.

用户代理不能将type属性视为权威的 - 在获取资源时,用户代理不得使用type属性来确定其实际类型。仅使用实际类型(如下一段中所定义)来确定是否应用资源,而不是上述假设类型。

#5


0  

If you dont use the type it will not validate

如果您不使用该类型,它将无法验证

#6


0  

According to w3 this is required.

根据w3,这是必需的。

Even if new browsers can manage without specifying the tag (by using a default) it is still better to leave it in for backwards compatibility with older browsers.

即使新浏览器可以在不指定标记的情况下进行管理(通过使用默认值),最好将其保留以便与旧版浏览器向后兼容。

#1


21  

Most people are used to HTML 4/XHTML and before, where the type attribute is required for these elements.

大多数人习惯于使用HTML 4 / XHTML,之前需要这些元素的type属性。

In regards to HTML 5, these are indeed optional and the spec gives a default, depending on the element.

关于HTML 5,这些确实是可选的,并且规范给出了默认值,具体取决于元素。

For the script tag, this defaults to text/javascript:

对于脚本标记,默认为text / javascript:

If the language is not that described by "text/javascript", then the type attribute must be present

如果语言不是“text / javascript”描述的语言,则必须存在type属性

For the style tag, this defaults to text/css:

对于样式标记,默认为text / css:

The default value for the type attribute, which is used if the attribute is absent, is "text/css".

如果属性不存在,则使用type属性的默认值为“text / css”。

So, not needed, as you stated. However, browser support and server setups can't always be relied on - being explicit is a good idea as it avoids such problems.

所以,不需要,如你所说。但是,不能总是依赖浏览器支持和服务器设置 - 明确是一个好主意,因为它避免了这样的问题。

And of course, not all browsers out there support HTML 5 - those that don't will use an earlier version where the attribute is required and your javascript/css might not get parsed in such browsers, meaning you end up with no CSS or javascript on older browsers, when a simple solution for backwards compatibility is to add the attribute.

当然,并非所有浏览器都支持HTML 5 - 那些不会使用早期版本的属性是必需的,你的javascript / css可能无法在这样的浏览器中解析,这意味着你最终没有CSS或javascript在较旧的浏览器上,当向后兼容性的简单解决方案是添加属性时。

#2


4  

The type attribute may not be required for HTML5 but it is required for other HTML Doc Types such as HTML 4.01 Strict. I'd also say that anything making the code/document clearer for the developer is really only ever a good thing.

HTML5可能不需要type属性,但HTML 4.01 Strict等其他HTML Doc Types需要它。我还说,任何能让开发人员更清楚代码/文档的东西都是一件好事。

If that means being explicit about the type of script being used or the type of style, I'd use it.

如果这意味着要明确使用的脚本类型或样式类型,我会使用它。

#3


1  

That's not good. In XHTML, the type attribute is strictly required. Although browsers may be lenient, that's no reason to break convention.

这不好。在XHTML中,严格要求type属性。虽然浏览器可能很宽松,但没有理由违反惯例。

#4


1  

The type attribute is indeed not required for HTML5, but including it doesn't break validation, so you can convert to HTML5 from either HTML 4 or XHTML 1, and still have your <script> and <style> tags validate.

HTML5确实不需要type属性,但包括它不会破坏验证,因此您可以从HTML 4或XHTML 1转换为HTML5,并且仍然可以验证

<link> tags also do not need a type attribute (emphasis added):

标签也不需要类型属性(强调添加):

The type attribute gives the MIME type of the linked resource. It is purely advisory. The value must be a valid MIME type.

type属性提供链接资源的MIME类型。这纯粹是建议性的。该值必须是有效的MIME类型。

For external resource links, the type attribute is used as a hint to user agents so that they can avoid fetching resources they do not support. If the attribute is present, then the user agent must assume that the resource is of the given type (even if that is not a valid MIME type, e.g. the empty string). If the attribute is omitted, but the external resource link type has a default type defined, then the user agent must assume that the resource is of that type. If the UA does not support the given MIME type for the given link relationship, then the UA should not obtain the resource; if the UA does support the given MIME type for the given link relationship, then the UA should obtain the resource at the appropriate time as specified for the external resource link's particular type. If the attribute is omitted, and the external resource link type does not have a default type defined, but the user agent would obtain the resource if the type was known and supported, then the user agent should obtain the resource under the assumption that it will be supported.

对于外部资源链接,type属性用作用户代理的提示,以便它们可以避免获取它们不支持的资源。如果该属性存在,则用户代理必须假定该资源属于给定类型(即使该资源不是有效的MIME类型,例如空字符串)。如果省略该属性,但外部资源链接类型已定义默认类型,则用户代理必须假定该资源属于该类型。如果UA不支持给定链接关系的给定MIME类型,则UA不应获取该资源;如果UA确实支持给定链接关系的给定MIME类型,则UA应在为外部资源链接的特定类型指定的适当时间获取资源。如果省略该属性,并且外部资源链接类型没有定义默认类型,但是如果类型已知且受支持,则用户代理将获取资源,那么用户代理应该假设它将获得资源得到支持。

User agents must not consider the type attribute authoritative — upon fetching the resource, user agents must not use the type attribute to determine its actual type. Only the actual type (as defined in the next paragraph) is used to determine whether to apply the resource, not the aforementioned assumed type.

用户代理不能将type属性视为权威的 - 在获取资源时,用户代理不得使用type属性来确定其实际类型。仅使用实际类型(如下一段中所定义)来确定是否应用资源,而不是上述假设类型。

#5


0  

If you dont use the type it will not validate

如果您不使用该类型,它将无法验证

#6


0  

According to w3 this is required.

根据w3,这是必需的。

Even if new browsers can manage without specifying the tag (by using a default) it is still better to leave it in for backwards compatibility with older browsers.

即使新浏览器可以在不指定标记的情况下进行管理(通过使用默认值),最好将其保留以便与旧版浏览器向后兼容。