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

E

ethandbrown

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
 
M

Martin Honnen

(e-mail address removed) 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.
 
E

ethandbrown

Martin said:
(e-mail address removed) wrote:




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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top