7

I noticed today while doing some testing that the way I close my <script> tag either makes or breaks my page. For example, this works:

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

but this does not:

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

The file appears to show up when I use IE's Developer Tools, but it seems like it just gets ignored. Has anyone ever seen this or know why it might be happening? Thanks in advance!

lhan
  • 4,585
  • 11
  • 60
  • 105

3 Answers3

14

You must include a closing script tag. The script element is not self closing, even when you're only including an external script.

KatieK
  • 13,586
  • 17
  • 76
  • 90
7

The <script> tag can only be self-closing in truly valid XHTML documents – that is, a XHTML page served with the Content-Type of application/xhtml+xmland when viewed in a supporting browser (IE8 does not qualify; IE9+ does).

In all other HTML documents, (regardless of what DOCTYPE is declared), the <script> tag is not self-closing and therefore must be closed with a </script>.

Read more in this very detailed answer.

Community
  • 1
  • 1
josh3736
  • 139,160
  • 33
  • 216
  • 263
2

I have also noticed you always need the </script>. It's probably because it requires content between the tags ("" counts), even though you're using src.

Andrea
  • 19,134
  • 4
  • 43
  • 65