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:
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.
No, the 'type' attribute is not needed in tags. Unless you want your code to validate. We've been omitting it for years at work, and it's never been an issue.
It's not required with the HTML5 spec, but for older versions of HTML is it required. Type stands for The MIME type of the style sheet.
The type attribute in JavaScript is optional since the introduction of HTML5 brought some new improvements. JavaScript became the default language for HTML5 and modern browsers.
It is not valid to have the same attribute name twice in an element.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With