Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include JavaScript file in HTML won't work as <script .... />

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

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

This works fine - however as there's nothing between the two tags, I wanted to change it to:

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

If I do this, it doesn't work - nothing after that line loads on the page.

Any ideas why? Thanks

like image 207
Freddie Avatar asked Jan 04 '11 12:01

Freddie


2 Answers

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

like image 189
Dekker500 Avatar answered Oct 04 '22 13:10

Dekker500


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

like image 21
Sean Patrick Floyd Avatar answered Oct 04 '22 14:10

Sean Patrick Floyd