By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,629 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,629 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ignac Vucko wrote:
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
Ivo
"Ignac Vucko" wrote
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, but surely your <div> element belongs in the body.
</head>
<body>blah</body>
<html>


hth
--
Ivo
http://4umi.com/web/javascript/
Jul 23 '05 #3

P: n/a
thanks for your responses!
A <div> element in the head? That is a problem of illegal,
not to say impossible, nesting.
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.
Whilst that is syntactically legal, it would create an invalid
document tree: DIV elements cannot be descendants of the HEAD

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

Jul 23 '05 #4

P: n/a
Ivo
<ig********@gmail.com> wrote
A <div> element in the head? That is a problem of illegal,
not to say impossible, nesting.

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.

--
Ivo
Jul 23 '05 #5

P: n/a
> Don't jump into that trap.
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/inte....html#h-18.2.4
(since <title> MUST be in a <head> section)

Ignac

Jul 23 '05 #6

P: n/a
ig********@gmail.com wrote:
Don't jump into that trap.


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/inte....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.

--
Rob
Jul 23 '05 #7

P: n/a
ig********@gmail.com wrote:

[snip]
It is interesting though that the spec itself
indirectly indicates that such markup is valid.
http://www.w3.org/TR/REC-html40/inte....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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.