Are type attributes needed for SCRIPT, STYLE, and LINK elements?

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

Items

Script:

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

Are type attributes needed in popular browsers today?

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

What happens if you do not turn them on?

Note. The answer should cover both HTML5 and XHTML doctrines if there is a difference in behavior between them.

+6
source share
4 answers

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


In the HTML5 type script tag:

type . This attribute defines the scripting language of the embedded code. in a script element or referenced through the src attribute of the elements. This is indicated as a MIME type; Examples of supported MIME types include text / javascript, text / extract, application / javascript, and Application / ECMAScript. If this attribute is missing, the script is processed as JavaScript.

HTML4 and XHTML require W3C standards .

For style and link type :

In HTML5, the type attribute is no longer required. The default value is Text / CSS.

+6
source

Short answer

No, it is not.

Long answer

Over time, the browser sets the default type=text/javascript to script elements, etc. when you omit the type attribute. HTML5 makes this official, but every browser has supported it a long time ago.

In fact, what type of doctype you use does not affect browser behavior, only the W3C specification. A good and short HTML document type has been the standard doctrine for many years in regular browsers. HTML5 just makes these nice things official.

Thus, you can use it, and no browser will scream or work, only a validator.

+4
source

In new browsers (supporting HTML5), the type attribute is optional, and if it is not specified, sctipt will understand the default value, since text / javascript and style will be understood as text / css

+1
source

They are not currently required . Since all browsers assume by default what you expect.

But they may become important in the future .

For example, when Google Dart is fully integrated into the browser, and then we have an alternative to JavaScript. In the same way, as soon as someone offers a good CSS replacement, we will need it. And it is entirely possible that Dart will become "JavaScript 2.0". And this bastard language, which JavaScript certainly needs a more reasonable replacement. I think we all use it because there is no alternative that cross browser works (and in fact, not even one that works in one browser, I think ...).

So, if you plan to live on your site for a year: do not worry. If you plan to use the site for 10 years, try to be detailed about your types of content, formats and versions.

+1
source

Source: https://habr.com/ru/post/926474/


All Articles