Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Type Attributes on SCRIPT, STYLE, and LINK elements still needed?

Tags:

html

browser

css

You will see many sites with the following type of code:

Script elements:

<script type="text/javascript">
   //javascript here
</script>

Link elements:

<link rel="stylesheet" href="url.css" type="text/css" media="all" />

Style elements:

<style type="text/css">
   /* CSS */
</style>

My question is this:

Are the type attributes needed in the popular browsers today?

(Popular meaning IE 8+, Firefox, Webkit, Opera and Chrome)

What happens if you do not include them?

Note: The answer needs to cover both HTML5 and XHTML doctypes if there is a difference in behavior between the two.

like image 297
L84 Avatar asked Sep 28 '12 05:09

L84


3 Answers

In short, they are not required since HTML5, but are required by W3C standards in HTML4/XHTML.


In HTML5 type of script tag:

type - This attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.

in HTML4 and XHTML it's required by W3C standards.

For style and link type:

In HTML5, the type attribute is no longer required. Default value is "text/css".

like image 137
loler Avatar answered Sep 21 '22 15:09

loler


Short answer

No, they aren't.

Long answer

For a long time, browser sets the default type=text/javascript at script elements, ect. when you omitted the type attribute. HTML5 makes this official, but every browser supported it long time ago.

In fact, which doctype you use isn't affecting the browsers behaviour, only the W3C specs. The nice and short HTML5 doctype is the standard doctype for years in normal browsers. HTML5 just makes those nice things official.

So you can use it and no browser will scream or fail, only the validator.

like image 40
Wouter J Avatar answered Sep 18 '22 15:09

Wouter J


In Newer browsers (supporting HTML5) type attrib is optional and if not given it sctipt will understand default as text/javascript and style will understand as text/css

like image 36
Kumar Gaurav Avatar answered Sep 21 '22 15:09

Kumar Gaurav