JavaScript document.writeln and Chrome

Be careful if you are using Chrome and thinking of doing a quick javascript pop window to do some outputting.

 

<html>
 <head>
 <script type="text/javascript">
 function newWindow() {
   debugWindow =   window.open('','debugWindow','width=400,height=200,scrollbars=yes,resizable=yes');
   debugWindow.document.writeln('this is a new window');
 }
 </script>
 </head>
 <body onload='newWindow();'>
 </body>
</html>

 

This code was giving me a pop window but it was empty! I was sitting there for a long time trying to figure out why the above code is not printing anything in my Chrome 3.0.195.27. It was working perfectly fine in FireFox and IE. It took me 4 hour before I figured out what was required in addition:

 

 

<html>
 <head>
 <script type="text/javascript">
 function newWindow() {
   debugWindow =   window.open('','debugWindow','width=400,height=200,scrollbars=yes,resizable=yes');
   debugWindow.document.writeln('this is a new window');
   debugWindow.document.close();
 }
 </script>
 </head>
 <body onload='newWindow();'>
 </body>
</html>

 

 

Notice the debugWindow.document.close(); line. This has to do with how Chrome handles event handles and document parsing during <script>-block parsing phase. I assume FireFox and IE do this alittle differently hence you see output right away without having to call debugWindow.document.close(); Perhaps this behaviour in Chrome will change down the line as for right now if you were writing that function strictly just to do some quick javascript debugging I recommend using: console.log() which works in FireFox’ FireBug as well as Chrome internal JavaScript console.

 

Here is another problem I found related to this:

http://stackoverflow.com/questions/762416/why-isnt-chrome-running-this-javascript

Comments are closed.