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

前端 未结 4 463
时光取名叫无心
时光取名叫无心 2021-01-12 02:41

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

Script elements:



        
相关标签:
4条回答
  • 2021-01-12 03:22

    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".

    0 讨论(0)
  • 2021-01-12 03:24

    They are currently not required. Because all browsers assume the default that you would expect.

    But they may become important in the future.

    For example, when Google Dart becomes fully integrated in the browsers, and we then finally have an alternative to JavaScript. Similarly, once someone proposes a nice replacement for CSS, we will need this. And it may well be that Dart becomes "JavaScript 2.0". And the bastard language that JavaScript is surely needs a more sane replacement. I guess we are all only using it, because there is no alternative that works cross-browser (and in fact, not even one that works in a single browser, I guess ...).

    So if you plan your site to live for a year: don't bother. If you plan to have the site running for 10 years, try to be verbose about your content types, formats and versions.

    0 讨论(0)
  • 2021-01-12 03:26

    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

    0 讨论(0)
  • 2021-01-12 03:37

    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.

    0 讨论(0)
提交回复
热议问题