473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>documen t.writeln("<div id='mydiv'>stuf f</div>");</script>
</head>
<body>blah</body>
<html>

cheers,
Ignac
Jul 23 '05 #1
7 5561
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>documen t.writeln("<div id='mydiv'>stuf f</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>documen t.writeln("<div id='mydiv'>stuf f</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********@gma il.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********@gmai l.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>Tes t 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********@gmai l.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
2045
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 test): <script LANGUAGE="javascript"> <!-- function hiThere() { alert("hi");
17
2184
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 "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DT¬D/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
2300
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 = document.createElement("a");
15
2253
by: Frances | last post by:
<html> <head> <script> function doIt() { var list = document.forms.product; var selItem = list.options.value; ^^^^^^^ </head>
3
1478
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 attempt Page.Response.AppendHeader(someStringBuilder); result: Returned nothing on the page. Second attempt
3
1408
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 seems Render only sends items into the <BODY>. Actually, it would be nice if I had control over the entire rendering process, including the whole <HEAD>.
1
4762
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. But can I do so since the control would not be withing a <form> tag in the aspx page? Can I do like so?:
1
1941
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'> Object=window.open('test.html','test1','name="test1"'); Object.focus()
19
11256
by: Scott M. | last post by:
Why do we need runat="server" on our <headtags?
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.