external javascript breaks document.write() recursion

Discussion in 'Javascript' started by Semmel, Aug 23, 2006.

  1. Semmel

    Semmel Guest

    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
    Semmel, Aug 23, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jullag
    Replies:
    20
    Views:
    305
    Frances
    Sep 22, 2005
  2. Replies:
    2
    Views:
    331
  3. Replies:
    6
    Views:
    201
    Thomas 'PointedEars' Lahn
    Feb 22, 2006
  4. Frank Peterson
    Replies:
    9
    Views:
    228
    David Mark
    Jun 28, 2009
  5. Replies:
    8
    Views:
    737
    John Reye
    Apr 26, 2012
Loading...

Share This Page