Charles' Log: week of 30 April 2001

  1. May (Thurs). 

    Nasty, vicious, Netscape 4 document.write bug
    There are a number of very subtle bugs that the javascript programmer can encounter when using document.write() to produce dynamically generated HTML.  A search on google for "netscape document.write bug" will product 15 pages worth of links on the subject.

    I had the misfortune to encounter a new one: for reasons as yet unknown, using many document.write()s to generate HTML in the midst of a page, eventually froze the browser completely.  (Same results on Windows, Mac, and Solaris Netscape 4.x browsers!) 

    The good news is that I could work around the problem by accumulating the text in a string, and displaying it in a single document.write call; also for reasons unknown, that worked just fine.

    E.g., something like the text below, instead of many document.write()s.

       <html>
       <body>
       <script language="javascript">
          var docstr = "";
          function dw (str) { docstr += str; }
    
          dw ('<h1>hello, world</h1>');
          dw ('Write something<br>');
          dw ('Write something else<br>');
          ...
    
          document.write (docstr);
       </script>
       </body>
       </html>