5

Possible Duplicate:
Why don't self-closing script tags work?

I'm trying jquery now. When I included the jquery.js as follows

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

the code doesn't worked properly. Actually it is just a simple hello world program. I just called a jQuery specific function. But that was not working if I include the file as above. But when I changed the closing like this

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

the code worked well. What is the difference?

Community
  • 1
  • 1
theB
  • 2,048
  • 2
  • 20
  • 29

4 Answers4

7

<script /> is valid XML, but invalid HTML.

If you serve your pages as text/xml the browser will correctly load them.

Frankie
  • 24,627
  • 10
  • 79
  • 121
4

Script tags simply cannot be self-closing.

4

Before anyone else mentions it, HTML is NOT XHTML. It's not even a subset. HTML evolved out of SGML and it has its own rules. One of those rules is that certain tags need to be closed and certain tags don't. There's actually no such thing as <script /> or even <br />, that's an XML concept and they really don't belong in HTML at all.

The only reason these things sometimes work is because page authors do things wrong and some browsers try to be nice about it.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
3

A few years back I also faced the same problem and at the end my conclusion after doing all the search is that there is no logical answer to this.

So I consider it an exception without reason

M. Ahmad Zafar
  • 4,881
  • 4
  • 32
  • 44