Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > inline document.write(html) within <head> tag safe?

Reply
Thread Tools

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

 
 
Ignac Vucko
Guest
Posts: n/a
 
      03-11-2005
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
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      03-11-2005
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.
 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      03-11-2005
"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/


 
Reply With Quote
 
ignacvucko@gmail.com
Guest
Posts: n/a
 
      03-11-2005
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

 
Reply With Quote
 
Ivo
Guest
Posts: n/a
 
      03-11-2005
<> 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


 
Reply With Quote
 
ignacvucko@gmail.com
Guest
Posts: n/a
 
      03-11-2005
> 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

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-11-2005
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
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      03-11-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
render a .tag from within a .java tag? cliff.helsel@kadooinc.com Java 0 07-23-2008 03:01 PM
Select text within a div tag by clicking on content of div tag or a button? visu Javascript 4 11-22-2006 06:25 AM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Select text within a div tag by clicking on content of div tag? M Wells Javascript 0 10-06-2004 11:04 AM
netscape 4.7: onmouseover not working when i put <a> tag within a div tag Eqbal Z Javascript 6 08-27-2003 12:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57