Unclosed HTML tag not working in document.write()

R

Roy Smith

I'm just learning js, and am confused by some behavior I'm seeing in
Chrome (version 5.0.375.127, OSX). This may be more of a browser issue
than a javascript issue, so my apologies if this is the wrong forum.

I've got the following little HTML file.

<html>
<head>
<script type="text/javascript">
function message()
{
document.write("<p>My message");
}
</script>
</head>

<body onload="message()">
</body>
</html>

when I load it into Chrome, I get a blank page. Loading this HTML:

<p>My message

displays "My message". A little experimenting shows that if I close the
<p> tag with </p> in the js version, "My message" gets displayed.

I don't understand why the browser cares about the closing tag when the
HTML comes from a script, and doesn't care when it's read from the file
directly.
 
E

Evertjan.

Roy Smith wrote on 28 aug 2010 in comp.lang.javascript:
I've got the following little HTML file.

<html>
<head>
<script type="text/javascript">
function message()
{
document.write("<p>My message");
}
</script>
</head>

<body onload="message()">
</body>
</html>

when I load it into Chrome, I get a blank page.

According to specs this is neaded:

========================
<html>
<head>
<script type="text/javascript">
function message() {
document.open();
document.write("second page");
document.close();
};
</script>
</head>

<body onload="message()">
first page
</body>
</html>
========================

Now IE is so sloppy in that the open() and close() are presumed,
chrome only allows you to skip only the open() statement.

The document.write()ing after the page is finished always overwrites the
page content, btw.
 
R

Roy Smith

Evertjan. said:
Now IE is so sloppy in that the open() and close() are presumed,
chrome only allows you to skip only the open() statement.

Ah, cool. Thanks. I've been working from http://www.w3schools.com/js/,
which gave the example of the write() without the open() or close(). Is
there a better tutorial you might suggest? One which doesn't give bogus
examples? :)
 
N

news.virginmedia.com

Roy Smith said:
Ah, cool. Thanks. I've been working from http://www.w3schools.com/js/,
which gave the example of the write() without the open() or close(). Is
there a better tutorial you might suggest? One which doesn't give bogus
examples? :)

Hi,
Just been following another thread on this newsgroup and it seems
document.write() is not supported in XHTML , so no point in using it if you
want to write future proof and totally compliant code.
I'm a bit rusty on this stuff and haven't been practising for a few years,
hence my reading newsgroups to get back into it, but DOM programming has
always has these cross browser issues and lack of strict standards, to write
total cross browser compliant code is very difficult and takes alot of
research and testing to get it right. ALso you need a couple of pc's ( a mac
and a pc ) for testing purposes.
I think generally that w3schools is ok site for picking up general concepts
but you need to browse web and find 1 or two other cross references sites
that you like to get a wider picture. I prefer to learn from book myself and
back up from websites. w3schools seems ok for step by step tutorials but
there will be many good sites out there that will be better for references
and picking up tips and tricks.

gl
 
D

Doug Gunnoe

Ah, cool.  Thanks.  I've been working fromhttp://www.w3schools.com/js/,
which gave the example of the write() without the open() or close().  Is
there a better tutorial you might suggest?  One which doesn't give bogus
examples? :)

w3schools doesn't have the best reputation. A few in this newsgroup
have pointed out errors on their site.
 
E

Evertjan.

news.virginmedia.com wrote on 29 aug 2010 in comp.lang.javascript:

I would not advice you to a tutorial, Roy. Javascript, like any language,
is learned by trial and error, and testing the work of others. That is
NOT copying code that you do not understand, but analyzing it hands-on to
further your skills. and scrutinizing the specs that are there, even
though they only are the blueprints and not describing the diverse final
builds.

Tutorials may be handy to get a specific point explained, but like in all
science and technology, be wary of anything said as true [including this
posting, ofcourse], it often isn't.

Javascript is a technology, but programming should be a science.

There are absolute sciences, where all parts work as specified, like in
chess or mathematics, and there are sciences where the specs are
incomplete, like in medicin or Javascript.
Just been following another thread on this newsgroup and it seems
document.write() is not supported in XHTML , so no point in using it
if you want to write future proof and totally compliant code.

1 Which seems to me total nonsense, since xhtml is just a special way of
writing html, and should have nothing to do with Javascript or it's DOM
interaction.

Can you show us an url where your statement is shown in practice?

Like I explained above, the hearsay does not convince me.

2 Why would you worry about xhtml, what would be the advantage and when?

Not just because it looks "cool" to write <br />, I hope?
 
T

Thomas 'PointedEars' Lahn

Bull^WUtter nonsense.
Ah, cool.

No, it's pitiful.

Thank him for what, spreading FUD?
I've been working from http://www.w3schools.com/js/,
Don't.

which gave the example of the write() without the open() or close().

It would not make much of a difference anyway. document.write() after the
document has been loaded (the `load' event occurs, so the code in the `body'
element's `onload' event-handler attribute value is executed by the script
engine) is *supposed* to open a new stream and create a new document. (Only
document.close() would be necessary then.)

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268>

If you want to write text into a document dynamically with document.write(),
you must not utilize the `load' event of that document. Instead, use a
`script' element within the `body' element --

<body>
<script type="text/javascript">
message();
</script>
</body>

-- or if you want to insert/append content after the document has been
loaded, use the appendChild() method (this is currently necessary if you
serve XHTML to be parsed by an XML parser):

function message()
{
var p = document.createElement("p");
if (p)
{
var text = document.createTextNode("My message");
if (text)
{
p.appendChild(text);
document.body.appendChild(p);
}
}
}

<http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-1950641247>

This has been discussed ad nauseam here.
Is there a better tutorial you might suggest?
One which doesn't give bogus examples? :)

There are plenty of them.

See also <http://jibbering.com/faq/>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top