inline document.write(html) within <head> tag safe?

I

Ignac Vucko

Is writing a document *during* page load safe
and supported for all 4th and 5th generation browsers?
If not, can you show me a specific example/browser
where it causes problems?

<html>
<head>
<script>document.writeln("<div id='mydiv'>stuff</div>");</script>
</head>
<body>blah</body>
<html>

cheers,
Ignac
 
M

Michael Winter

Ignac said:
Is writing a document *during* page load safe
and supported for all 4th and 5th generation browsers?

Considering /all/ modern browsers is rather difficult. However, of the
common ones that support client-side scripting, yes.

[snip]
<script>document.writeln("<div id='mydiv'>stuff</div>");</script>

Whilst that is syntactically legal, it would create an invalid
document tree: DIV elements cannot be descendants of the HEAD element.

[snip]

Mike
 
I

Ivo

Is writing a document *during* page load safe
and supported for all 4th and 5th generation browsers?

Almost by definition, writing a document IS a page load. Invoking a
document.write() at any time after page load destroys the current page, in
other words: during page load is the only time that you can safely use it if
what you want is to add to the current document.
If not, can you show me a specific example/browser
where it causes problems?

<html>
<head>
<script>document.writeln("<div id='mydiv'>stuff</div>");</script>

A <div> element in the head? That is a problem of illegal, not to say
impossible, nesting. You can document.write() all sorts of <link> or <meta>
elements in the head said:
</head>
<body>blah</body>
<html>

hth
 
I

ignacvucko

thanks for your responses!

This is what I thought, until I tried it.
I tested with >=Opera5, >=IE4, >=NN4, Firefox
....and to my amazement all not only accepted
the syntax, but rendered it properly.
element.

It appears that all the browsers I have
tried so far prepend the written html into
the <body> tag - rendering a valid doc tree.

So, my question remains:

Can anybody tell me or provide me an example
where doing this would cause *any* problems
in 4th or 5th generationi browsers?
(eg: javascript errors, alerts, improper rendering,
overriting of the actual body, page loading issues,
...anything)

Ignac
 
I

Ivo

This is what I thought, until I tried it.
I tested with >=Opera5, >=IE4, >=NN4, Firefox
...and to my amazement all not only accepted
the syntax, but rendered it properly.
It appears that all the browsers I have
tried so far prepend the written html into
the <body> tag - rendering a valid doc tree.

Be that as it may, there is no reason to do it this way, to rely on it. None
that I can think of. The phrase "rendered it properly" really puzzles me. If
you want the div in the head, it would be proper for it not to render, as
head content is not supposed to render at all!

Browsers are designed to second-guess the author's intention when faced with
questionable markup. They call that a feature, and the smarter the browser,
the more careless the author can be. Don't jump into that trap.
So, my question remains:

It has been answered a couple of times now.
 
R

RobG

Agreed. thanks for responding.

It is interesting though that the spec itself
indirectly indicates that such markup is valid.
http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.4
(since <title> MUST be in a <head> section)

Ignac

I disagree with your assumption. The <head> tag is optional, as
is the <body> tag. Therefore the example given:

"<TITLE>Test Document</TITLE>
<P><B>Hello World!</B>"

Does not mean you can put script inside the head, but that you
can put script into the HTML element without a head or body tag.
 
M

Michael Winter

(e-mail address removed) wrote:

[snip]
It is interesting though that the spec itself
indirectly indicates that such markup is valid.
http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.4
(since <title> MUST be in a <head> section)

It does not. As Rob said, both the start and end tags of the HEAD,
BODY, and HTML elements are optional. That doesn't mean to say that
the elements are optional, but that they don't need to be explicitly
included.

<TITLE>Test Document</TITLE>
<P><B>Hello World!</B>

When the TITLE element, a HEAD content element, is encountered a user
agent should infer the presence of HTML and HEAD. When the P element,
a BODY content elements, is encountered a user agent should infer the
closure of the HEAD element, and the start of the BODY element.

I thought this process was described in the specification, but I can't
seem to find it at the moment.

Mike
 

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,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top