external javascript breaks document.write() recursion

S

Semmel

Hi all,

I'd like to know if you agree with my implications or if there is a
better way to do things.

This is the problem:

We write a new page xyz to the current browser window by using the well
known command sequence
document.open();document.write(xyz);document.close();.

If now the string xyz itself contains a javascript tag which
references an external JS file à la <script
src="http://somewhere/extern.js"></script>, further document.write()
commands, contained in the xyz string or even in the external JS will
be extecuted by the browser not until the complete xyz page was
processed.
This means that the output of those document.write() commands will not
reach the proper location inside the xyz page. The Internet Explorer
sticks them to the end of the page, while Mozilla as its predecessor
Netscape4, shifts in as well known a document.open() before the delayed
document.write() effecting that the output is brought onto a new blank
page. (Secondary Mozilla sits there on the blank page waiting for a
document.close() which will never come from us since we never caused
this document.open()!)

This should demonstrate the above:
--- File 'loader.html' ----
<html>
<head>

<script language="JavaScript">
function writeNewPageIntern()
{
document.open();
document.write('<html><body><h1>WrittenPage Title</h1><hr><script
language="JavaScript"> document.write("dynamic contents (inside
string)")<\/script><hr></body></html>');
document.close();
}

function writeNewPageExternJS()
{
document.open();
document.write('<html><body><h1>WrittenPage Title</h1><hr><script
language="JavaScript"
src="http://localhost/test/extern.js"><\/script><hr>end of
page</body></html>');
document.close();
}

function writeNewPageExternJSIntern()
{
document.open();
document.write('<html><body><h1>WrittenPage Title</h1><hr><script
language="JavaScript"
src="http://www.no-such-server.ddr/halb/Cgi/extern.js"><\/script><script
language="JavaScript"> document.write("dynamic contents (inside
string)")<\/script><hr>end of page</body></html>');
document.close();
}
</script>

</head>
<body>

<h1>LoaderPage Title</h1>

<form action="javascript:writeNewPageIntern()"><input type="submit"
value="writeNewPageIntern()"></form>

<form action="javascript:writeNewPageExternJS()"><input type="submit"
value="writeNewPageExternJS()"></form>

<form action="javascript:writeNewPageExternJSIntern()"><input
type="submit" value="writeNewPageExternJSIntern()"></form>
</body>
</html>
--- File end ---

--- File extern.js ---
document.write("dynamic contents (extern JS)");
--- File End ---

The first function (writeNewPageIntern()) is successful and
demonstrates that recursive calls of document.write() are troublefree.

The second function shows the problem with document.write() commands in
the external JS file. (Please adjust in the code the location of the
external JS file on your system.)

The third function demonstrates that recursion is broken for Mozilla
even if the external JS can not be found.

Has anyone already encountered such behaviour?

A straight-forward workaround could consist in preloading the external
JS files somehow in strings and insert them into the xyz string before
we do our document.write(xyz).

Best regards
Semmel
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top