View Full Version : Resolved document.write and page never finishes loading

Steve Comstock
11-21-2010, 06:55 PM
So I have an HTML page with javascript that takes input values, computes some results, and then puts these results in a table. Works great. Now I realize some users might want to print just the results table. So I add a button that allows the user to request the table in a separate window; clicking the button invokes a script to re-create the table in a separate window. Works fine except:

* In the status bar, the window appears to never finish loading
* If you go to File -> Print then nothing seems to happen; however,
if you close the generated window _then_ the print dialog appears;
the window you want to print, however, is gone.

Any suggestions?

The code invoked by clicking the newly added button:

function grabResults()
var w = window.open("","ROIResults"+winCount++,
var d = w.document;
d.write('<link href="style.css" title="compact" rel="stylesheet" type="text/css" />');
d.write('<title>Return On Investment Calculations</title>');
d.write('<div id="logoholder">');
d.write(' <img src="TTFlogo2.gif" alt="Cannott display logo" "width="322" width="322" height="51"></p>');
d.write('<div id="header">');
d.write(' <h2>Return on Investment Estimator for Classroom Training</h2>');
d.write('<table rules="all" frame="box">');
d.write('<caption>Results - Estimated ROI</caption>');
d.write(' <tr><th>Course</th>');
d.write('<td class="center" id="courseName">'+cname+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Duration (Days)</th>');
d.write('<td class="center" id="courseDur">'+cdays+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Class Size</th>');
d.write('<td class="center" id="courseSize">'+csize+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Location</th>');
d.write('<td class="center" id="courseLoc">'+teach_loc+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Training Cost</th>');
d.write('<td class="justr" id="courseCost">'+TC+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Initial Value</th>');
d.write('<td class="justr" id="courseInitial">'+startValue+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Returned Value</th>');
d.write('<td class="justr" id="courseValue">'+RV+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Increase in Value</th>');
d.write('<td class="justr" id="courseIncr">'+Inc+'</td>');
d.write(' </tr>');
d.write(' <tr><th>Net ROI after costs</th>');
d.write('<td class="justr" id="courseROI">'+ROI+'</td>');
d.write(' </tr>');
d.write(' <tr><th>ROI Rate</th>');
d.write('<td class="justr" id="courseROIRate">'+ROIR.toFixed(2)+'%</td>');
d.write('</tr> '); */
d.write('<p class="in3">(All amounts are in US Dollars.)</p>');

return true;


Philip M
11-21-2010, 06:57 PM
document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

11-21-2010, 07:30 PM
d.write('</tr> '); */ should be d.write('</tr> ');

Fifth line from the bottom of the document write code.

Steve Comstock
11-21-2010, 07:47 PM
d.write('</tr> '); */ should be d.write('</tr> ');

Fifth line from the bottom of the document write code.

Yes, I just saw that, fixed it, and now it works! Thanks!