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
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