I ran into an interesting problem today that involved the need to “Save as” a web page that is 99% generated with Javascript document.writes. Of course, saving this page will give you only the javascript code, not what you are seeing on the screen – in my case, lots of table rows created by parsing a text file.
Having javascript read in a text file meant I was doing everything local, using FileSystemObject, so that was (A) Why I could not use a server side language to generate the file, and (B) good that I had the proper security to read and write files.
I found a lot of people pulling their hair out over this and no real solution. If you are working locally, can use FileSystemObject and need to save actual data, not javascript document.writes, then I think this may help you.
First off, I import my text file, parse the data, and document.write it into tables, such as:
document.write("<tr'>"); |
On screen, I may get something like:
| 1 | 01-002 | Artist | Need to remove border from the image. |
| 2 | 01-003 | Programmer | Make the next link disabled until they click on ‘Note’ |
| 3 | 01-003 | Programmer | Change the direction text to “Continue” when they click on ‘Note’ |
But if I try to save that page to the desktop, I get the javascript document.writes listed above… until you do this:
Surround your javascript with a div and give it an ID:
<body> |
Now, right after that div, call a function and pass it the div’s innerHTML:
<body> |
This is going to take the HTML contained within the div and send it to a function. It sends the javascript code, but also it sends the generated HTML table. Perfect! Now, in that function, I split the code on the closing </script> tag to get rid of all my JS code, and wrap what’s left in simple html, head, body tags and write a new .html file.
Now, all we have to do is redirect. So after our call to saveHTML, add:
<body> |
Oh, and the variable ‘html’ is just the location/filename I use to write the file to, and now redirect to. For instance
var html = “c://some/directory/to/write/to.html” |
Now, once we redirect, we are on a true HTML page with no content being generated by Javascript and we can successfully “Save as” and have a document with actual data in it.
Hope this helps.










