Include JavaScript file in HTML won't work as [removed]

前端 未结 3 885
盖世英雄少女心
盖世英雄少女心 2021-01-03 20:24

I\'d like to include a javascript file on every page of a site. I can do this with:

         


        
相关标签:
3条回答
  • 2021-01-03 21:08

    Unfortunately, the HTML specs for REQUIRE a closing tag...

    HTML Standard, section 18.2.1

    18.2.1 The SCRIPT element

    Start tag: required, End tag: required

    0 讨论(0)
  • 2021-01-03 21:13

    This is not a bug, it's standard behavior.

    Also, empty HTML elements are often not rendered:

    <div style="background:red"></div> displays, <div style="background:red" /> doesn't

    0 讨论(0)
  • 2021-01-03 21:14

    HTML doesn't support self closing tags. If you want to use them you need to use an xml based doctype AND serve the file as xml.

    XHTML or the xml serialisation of html5 would both work.

    Here is an example:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>XHTML5 Template</title>
        <meta charset="utf-8" />
        <script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js" />
      </head>
      <body>
      </body>
    </html>
    

    Save this in a file with a .xhtml extension and open it in a modern browser and the self closing tag will work.

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