473,320 Members | 1,879 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,320 software developers and data experts.

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

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


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

Similar topics

2
by: Jo_Calico | last post by:
I love the Dynamic Drive cross browser marquee script. I'd like to make the text loop immediately after completion, so the beginning runs right after the end (does that make sense?). Could anyone...
11
by: Wentink | last post by:
Does anybody have a simple script that let's me popup a picture from a thumbnail? The ones i found are all very very complicated and messy in the source... Thanks, Tintin
11
by: Jonny | last post by:
Netscape 7.02 is giving me a headache with a downloaded snow script. Starting with a blank page, I inserted the script and checked it in IE 6 and Netscape 7.02. Everything worked and looked fine. A...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
17
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.