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

Evaluating SCRIPT block in DHTML: document.write() problem

P: n/a
Hi All--

I'm a bit stymied here. I need to display arbitrary HTML obtained
through AJAX. The problem is when a <script> block is encountered
one can't use innerHTML to set the content, because the <script> block
won't be evaluated.

Googling around, a found the createContextualFragment() solution
which does execute script code. The following, for example, works:

var content = "<SCRIPT language='javascript' type='text/javascript'>" +
"\n"
+ 'alert("Hello")' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getElementById('testdiv');

var rng = document.createRange();
rng.setStartBefore(mydiv);
htmlFrag = rng.createContextualFragment(content);
while (mydiv.hasChildNodes())
mydiv.removeChild(mydiv.lastChild);
mydiv.appendChild(htmlFrag);
}

When fillDiv is called from the web page, an alert box pops up as it
should.

The problem I'm encountering is when the <script> block uses
document.write()
to modify content. Specifically, some of the code I'm encountering is
from
Javascript Tree-menu libraries that use document.write() to output
their menus.

A minimal implementation example of the problem can be shown by
replacing the "alert()"
call above with document.write():
var content = "<SCRIPT language='javascript' type='text/javascript'>" +
"\n"
+ 'document.write("<strong>Hello World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getElementById('testdiv');

var rng = document.createRange();
rng.setStartBefore(mydiv);
htmlFrag = rng.createContextualFragment(content);
while (mydiv.hasChildNodes())
mydiv.removeChild(mydiv.lastChild);
mydiv.appendChild(htmlFrag);
}

When fillDiv is called from the web page, the page flashes very
quickly,
showing "Hello World", but the page content is then immediately
replaced by
the original content.

As I have no control over the <script> blocks, does anyone have any
suggestions for
making the document.write() output behave correctly (be written to the
target DIV)
when executed within the contextual fragment?

By the way, I'm testing this on Firefox 1.5.

Thanks much,

--Ethan

Jun 24 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


et*********@gmail.com wrote:
var content = "<SCRIPT language='javascript' type='text/javascript'>" +
"\n"
+ 'document.write("<strong>Hello World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getElementById('testdiv');

var rng = document.createRange();
rng.setStartBefore(mydiv);
htmlFrag = rng.createContextualFragment(content);
while (mydiv.hasChildNodes())
mydiv.removeChild(mydiv.lastChild);
mydiv.appendChild(htmlFrag);
}

When fillDiv is called from the web page, the page flashes very
quickly,
showing "Hello World", but the page content is then immediately
replaced by
the original content.


How exactly do you call the fillDiv function?

In general note that createContextualFragment is Mozilla only, besides
the newly released Opera 9 also featuring it.

And do not expect any browser to behave the same if you mix W3C DOM Core
methods like appendChild to insert script with document.write.
document.write is useful to overwrite the complete document or to use it
while a document loads. Then the document.write happens where you call it.
There is however no clear definition as to what has to happen when you
have document.write in a dynamically inserted script. IE/Win for
instance only excutes such dynamically inserted script if you use the
defer attribute which rules out that document.write is used.
You will need to change your approach.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 25 '06 #2

P: n/a
Martin Honnen wrote:
et*********@gmail.com wrote:
var content = "<SCRIPT language='javascript' type='text/javascript'>" +
"\n"
+ 'document.write("<strong>Hello World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getElementById('testdiv');

var rng = document.createRange();
rng.setStartBefore(mydiv);
htmlFrag = rng.createContextualFragment(content);
while (mydiv.hasChildNodes())
mydiv.removeChild(mydiv.lastChild);
mydiv.appendChild(htmlFrag);
}

When fillDiv is called from the web page, the page flashes very
quickly,
showing "Hello World", but the page content is then immediately
replaced by
the original content.


How exactly do you call the fillDiv function?

In general note that createContextualFragment is Mozilla only, besides
the newly released Opera 9 also featuring it.

And do not expect any browser to behave the same if you mix W3C DOM Core
methods like appendChild to insert script with document.write.
document.write is useful to overwrite the complete document or to use it
while a document loads. Then the document.write happens where you call it.
There is however no clear definition as to what has to happen when you
have document.write in a dynamically inserted script. IE/Win for
instance only excutes such dynamically inserted script if you use the
defer attribute which rules out that document.write is used.
You will need to change your approach.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Hi Martin--

I agree that trying to get document.write() to work correctly in this
situation is likely futile, and I will try a different path. To answer
your first question, fillDiv is executed using the following HTML:

<a href=# onClick='fillDiv()'>Click to fill</a>
<div id=testdiv>

</div>

Thank you very much for taking the time to respond to my question.

--Ethan

Jun 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.