I would like to include a javascript file on every page of the site. I can do th...">

Include JavaScript file in HTML will not work as <script .... / ">

I would like to include a javascript file on every page of the site. I can do this with:

<script type="text/javascript" src="myFile.js" ></script> 

This works fine - however, since there is nothing between the two tags, I would like to change it to:

 <script type="text/javascript" src="myFile.js" /> 

If I do this, it will not work - nothing after this line is loaded onto the page.

Any ideas why? Thanks

+26
javascript html
Jan 04 2018-11-12T00:
source share
3 answers

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

HTML Standard Section 18.2.1

18.2.1 SCRIPT Element

Start tag: required ; end tag: required

+32
Jan 04 2018-11-12T00:
source share
— -

This is not a mistake, this is standard behavior.

In addition, empty HTML elements are often not displayed:

<div style="background:red"></div> , <div style="background:red" /> not

+3
Jan 04 2018-11-12T00:
source share

HTML does not support self-closing tags. If you want to use them, you need to use xype-based doctype AND serve the file as xml.

XHTML or xml html5 serialization will 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 the extension .xhtml and open it in a modern browser, and the closing tag itself will work.

+2
May 03 '13 at 18:33
source share



All Articles