Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 873
Félix Saparelli Avatar asked Jun 04 '11 19:06

Félix Saparelli


People also ask

Is type attribute necessary?

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.

Do you need type text CSS?

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.

Is type text/JavaScript necessary?

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.

Can an attribute be applied multiple times to the same element?

It is not valid to have the same attribute name twice in an element.


2 Answers

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.

like image 93
Oded Avatar answered Sep 27 '22 16:09

Oded


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.

like image 45
Jamie Dixon Avatar answered Sep 27 '22 15:09

Jamie Dixon