Do you really need to specify the type attribute? [duplicate]

走远了吗. 提交于 2019-12-18 18:51:07

问题


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

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:

  • The MIME type should be sent by the server,
  • JS and CSS are the defaults,
  • 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?

EDIT: This is explicitly about HTML5, not XHTML.


回答1:


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

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

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

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

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

The default value for the type attribute, which is used if the attribute is absent, is "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.




回答2:


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.

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




回答3:


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




回答4:


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.

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

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.

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.




回答5:


If you dont use the type it will not validate




回答6:


According to w3 this is required.

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.



来源:https://stackoverflow.com/questions/6239032/do-you-really-need-to-specify-the-type-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!