473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

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
7 5518
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
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
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ryan.mclean | last post by:
Hello everyone. Hope ya'll had a nice New Year. Anyway, my question is why won't this work? I must be doing something dumb . . . here is the code: in the body tag, I have this code (just to...
17
by: ccdrbrg | last post by:
This is a rather general subject, I apologize. I am new to XHTML, CSS, et al and I am having trouble understanding the DTD and xml namespace declarations. For example: <!DOCTYPE html PUBLIC...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
15
by: Frances | last post by:
<html> <head> <script> function doIt() { var list = document.forms.product; var selItem = list.options.value; ^^^^^^^ </head>
3
by: Sam Samnah | last post by:
Ok I have a bit of a problem with a Server control I am building. I need to write a client-side Javascript block between the open and closing Head tag. I have tried the following methods: first...
3
by: PJ6 | last post by:
I want to render this text into the <HEAD> section of a page (and perhaps mute any existing title declaration, or alter it to be this instead) - <title runat="server" id=HtmlTitle></title> It...
1
by: Frank | last post by:
I would like to create a custom control that generates everything that will be in the <head> </head> section. This would allow me to make changes on all pages withing a site in one location. ...
1
by: Danny Scaleno | last post by:
Hello, using IEC Controller, anybody knows how to capture the head part of an html page like this one? <html> <head> <script language='javascript'>...
19
by: Scott M. | last post by:
Why do we need runat="server" on our <headtags?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.