Javascript outside html?

Is it ever valid to put javascript after the closing html tag? As in:

<!doctype />
<html>
<head></head>
<body></body>
</html>
<script type='text/javascript'></script>

?

No. An HTML document can only contain one single element, which must be html.

The lowest some script is allowed to go is at the end of the body, just before the </body> tag:


<html>
<head>
</head>
<body>
    <script type="text/javascript"></script>
</body>
</html>

The common convention is also to use double quotes for HTML strings, and single quotes javsacript strings.

:bawling: :injured:

Hey, here’s an idea.

Pick and choose a doctype to use from the valid dtd list.

I would go for the following myself:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Either that or the XHTML equivalent.

Only if you have a reason for not going with HTML 4.01 strict. 99.9% of people should be using the HTML strict doctype for their new pages and the other 0.1% have a reason to use the XHTML strict one instead. No one has a reason to use any other doctype for new web pages as the others are only there for old pages that you haven’t fixed yet.

The best place to put a <script> tag is immediatley before the </body> tag. The second best place is immediately before the </head> tag. There is no reason to put any JavaScript anywhere else in your (X)HTML. You do need to remember that JavaScript for XHTML is significantly different from that for HTML though when choosing your doctype since with XHTML your JavaScript needs to specify namespaces for almost everything.

OK, thanks for all your answers. It was only supposed to be a quickly typed example to show you what I meant. I don’t use that for my doctype. Seriously. You can put away your pointy sticks now.

Why not - it is the correct doctype to use for new web pages, all the rest are for pages written last century or have extremely specialised uses

What made me want to cry was the ‘/>’ at the end of it. A doctype declaration is not an element, it’s an SGML markup declaration. It doesn’t use NET syntax even in X(HT)ML.

No, it’s not. If you’re thinking about HTML5 the ‘doctype declaration’ looks like this,

<!doctype html>

And it’s not something you should use for new web pages, except for experimental purposes. HTML5 is years (hopefully decades) away from being a W3C recommendation. The latest standard with widespread support is HTML 4.01. Last century or not.

You’re the only one to mention HTML 5. I was referring to the doctype pmw57 had already quoted which is for HTML 4.01 strict and I was disagreeing with the comment immediately under it that said “or XHTML equivalent”.

Thanks for that disagreement. My purpose with that statement about XHTML was to admit to people who do wish to use it, that it’s an option that’s available to them.

I myself consider that XHTML is not an option to be considered for web design work, but nevertheless do not wish the information that I provide about doctypes to be considered incomplete for the lack of information about it. This is why the comment about XHTML received such a small treatment from me, as if it were a postscript worthy of being only an afterthought.

Perhaps ‘disgreement’ was the wrong work - ‘clarification’ is probably closer to what I meant.

Sorry I got it wrong then. But your response was immediately below jylyn’s statement that he/she doesn’t ‘use that doctype’, which, from context, I interpret as referring to the <!doctype /> in the first post.