Icemanind
Icemanind

Reputation: 48696

<script> tag makes HTML contents disappear

This is such a wierd problem. I have the following extremly simple HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
       <title></title>
       <script type="text/javascript" src="jquery.js" />
    </head>
    <body>
    test
    </body>
</html>

If I run this page in a web browser, I get nothing at all, just an empty page. If I remove the script reference line entirely, then I get a blank page that displays "test", like it should. This tells me that there is some issue in the jquery.js file, but I have tried 3 different versions of jquery and the one I am using is the latest from the jquery.com website. For the life of me, I can't figure out what I'm doing wrong. I know it will turn out to be simple though.

Upvotes: 2

Views: 2876

Answers (4)

mdonoughe
mdonoughe

Reputation: 535

You're confusing HTML and XHTML.

In HTML, the script tag is not self closing, so you must write <script src="jquery.js"></script>.

In XHTML, you must set your XML namespace(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">) and send the document with the correct mime type.

Upvotes: 0

Mu Mind
Mu Mind

Reputation: 11204

The <script> tag is not self-closing, so you'll need a separate </script>:

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

See Why don't self-closing script tags work?

Upvotes: 2

Billy Moon
Billy Moon

Reputation: 58581

you need to close your script tag otherwise it thinks everything after is javascript to be executed...

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

Upvotes: 2

lonesomeday
lonesomeday

Reputation: 237965

script tags cannot be self-closing. You need the closing </script> tag:

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

Not including the closing tag means the whole content up to and including </html> is treated as the body of the script element. Because the document is then incomplete, your browser closes the script, head and html elements and inserts an empty body element.

Upvotes: 9

Related Questions