Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type attribute of SCRIPT and STYLE elements in HTML?

I heard (from Crockford) what type attributes on LINK and SCRIPT elements are superfluous when those elements are used to load external resources. (Because the HTTP response determines the content-type of the resource.)

<link rel="Stylesheet" href="foo.css">

<script src="foo.js"></script>

But what about the case when non-HTML code is placed inline inside the STYLE and SCRIPT elements?

<style>
    /* inline CSS rules */
</style>

<script>
    // inline JavaScript code
</script>

Is setting the type attribute in those cases recommended? Are there any issues when we choose to omit the type attribute?

like image 656
Šime Vidas Avatar asked Sep 08 '10 14:09

Šime Vidas


1 Answers

For HTML 4, the answer is simple: The type attribute is required for both <script> and <style>.

Authors must supply a value for this attribute; there is no default value for this attribute.

As far as I know, the default fallback in all browsers in its absence is text/javascript and text/css, respectively. It's widespread (though invalid) practice to not use the type attribute. I would still specify it to avoid browser problems.

HTML 5 accepts reality and declares these values as official defaults for <style> and <script>. I'm pretty sure this makes it okay to leave them off for inline content as well, which will be parsed using the correct content type automatically.

like image 60
Pekka Avatar answered Sep 22 '22 05:09

Pekka