HTML Script tag: type or language (or omit both)?

前端 未结 3 1083
天涯浪人
天涯浪人 2020-11-22 11:20

vs.



        
相关标签:
3条回答
  • 2020-11-22 12:00

    The language attribute has been deprecated for a long time, and should not be used.

    When W3C was working on HTML5, they discovered all browsers have "text/javascript" as the default script type, so they standardized it to be the default value. Hence, you don't need type either.

    For pages in XHTML 1.0 or HTML 4.01 omitting type is considered invalid. Try validating the following:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script src="http://example.com/test.js"></script>
    </head>
    <body/>
    </html>
    

    You will be informed of the following error:

    Line 4, Column 41: required attribute "type" not specified

    So if you're a fan of standards, use it. It should have no practical effect, but, when in doubt, may as well go by the spec.

    0 讨论(0)
  • 2020-11-22 12:05

    The type attribute is used to define the MIME type within the HTML document. Depending on what DOCTYPE you use, the type value is required in order to validate the HTML document.

    The language attribute lets the browser know what language you are using (Javascript vs. VBScript) but is not necessarily essential and, IIRC, has been deprecated.

    0 讨论(0)
  • 2020-11-22 12:14

    HTML4/XHTML1 requires

    <script type="...">...</script>
    

    HTML5 faces the fact that there is only one scripting language on the web, and allows

    <script>...</script>
    

    The latter works in any browser that supports scripting (NN2+).

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