473,769 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 createContextua lFragment() solution
which does execute script code. The following, for example, works:

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

function fillDiv()
{
var mydiv = document.getEle mentById('testd iv');

var rng = document.create Range();
rng.setStartBef ore(mydiv);
htmlFrag = rng.createConte xtualFragment(c ontent);
while (mydiv.hasChild Nodes())
mydiv.removeChi ld(mydiv.lastCh ild);
mydiv.appendChi ld(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='javas cript' type='text/javascript'>" +
"\n"
+ 'document.write ("<strong>He llo World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getEle mentById('testd iv');

var rng = document.create Range();
rng.setStartBef ore(mydiv);
htmlFrag = rng.createConte xtualFragment(c ontent);
while (mydiv.hasChild Nodes())
mydiv.removeChi ld(mydiv.lastCh ild);
mydiv.appendChi ld(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 2625


et*********@gma il.com wrote:
var content = "<SCRIPT language='javas cript' type='text/javascript'>" +
"\n"
+ 'document.write ("<strong>He llo World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getEle mentById('testd iv');

var rng = document.create Range();
rng.setStartBef ore(mydiv);
htmlFrag = rng.createConte xtualFragment(c ontent);
while (mydiv.hasChild Nodes())
mydiv.removeChi ld(mydiv.lastCh ild);
mydiv.appendChi ld(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 createContextua lFragment 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*********@gma il.com wrote:
var content = "<SCRIPT language='javas cript' type='text/javascript'>" +
"\n"
+ 'document.write ("<strong>He llo World.</strong>");' + "\n"
+ '</SCRIPT>' + "\n";

function fillDiv()
{
var mydiv = document.getEle mentById('testd iv');

var rng = document.create Range();
rng.setStartBef ore(mydiv);
htmlFrag = rng.createConte xtualFragment(c ontent);
while (mydiv.hasChild Nodes())
mydiv.removeChi ld(mydiv.lastCh ild);
mydiv.appendChi ld(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 createContextua lFragment 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='fillDi v()'>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
3392
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 tell me how to do this? Here's the link: http://www.calicosystems.com/pilla/index1.htm Here's the code: <script language="JavaScript1.2">
11
2841
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
1853
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 check on CPU usage (Windows Task Manager>Performance) gave a 0% to 2% reading for both browsers on a Pentium 4, 3.06GHz running XP. As I added text, images, tiled background and so on. I noticed the mouse was becoming jerky in Netscape 7....
3
1790
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 work when I add the scrolling text script. One script, the menu button script, has an "Onload" command and the other one does not. From what I know, and that's not much, I need to put both scripts in the "Onload" command. I just don't know how to...
3
2959
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 of the script gets executed. Can someone please give me a direction as to what I may be missing? Thanks.
17
34722
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 fine. However the HTML have a SCRIPT tag that the browser should process, but
0
9579
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
9416
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
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
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
9979
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
8861
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...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2810
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.