Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I *locally* save an .html file generated by javascript (running on a *local* .html page)?

So I've been researching this for a couple days and haven't come up with anything conclusive. I'm trying to create a (very) rudimentary liveblogging setup because I don't want to pay for something like CoverItLive. My process is: Local HTML file > Cloud storage (Dropbox/Drive/etc) > iframe on content page. All that works, and with some CSS even looks pretty nice despite the less-than-awesome approach. But here's the thing: the liveblog itself is made up of an HTML table, and I have to manually copy/paste the code for a new row, fill in the timestamp, write the new message, and save the document (which then syncs with the cloud and shows up in the iframe). To simplify the process I've made another HTML file which I intend to run locally and use to add entries to the table automatically. At the moment it's just a bunch of input boxes and some javascript to automate the timestamp and write the table row from the input data.

Code, as it stands now: http://jsfiddle.net/LukeLC/999bH/

What I'm looking to do from here is find a way to somehow export the generated table data to another .html file on my hard drive. So far I've managed to get this code...

    if(document.documentElement && document.documentElement.innerHTML){
       var a=document.getElementById("tblive").innerHTML;
       a=a.replace(/</g,'&lt;');
       var w=window.open();
       w.document.open();
       w.document.write('<pre>&lt;tblive>\n'+a+'\n&lt;/tblive></pre>');
       w.document.close();
       }
    }

...to open just the generated table code in a new window, and sure, I can save the source from there, but the whole point is to eliminate steps like that from the process.

How can I tell the page to save the generated code to a separate .html file when I click on the 'submit' button? Again, all of this happens locally, not on a server.

I'm not very good with javascript--and maybe a different language will be necessary--but any help is much appreciated.

like image 837
LukeLC Avatar asked Jan 08 '14 00:01

LukeLC


2 Answers

I suppose you could do something like this:

var myHTMLDoc = "<html><head><title>mydoc</title></head><body>This is a test page</body></html>";
var uri = "data:application/octet-stream;base64,"+btoa(myHTMLDoc);


document.location = uri;

BTW, btoa might not be cross-browser, I think modern browsers all have it, but older versions of IE don't. AFAIK base64 isn't even needed. you might be able to get away with

var uri = "data:application/octet-stream,"+myHTMLDoc;

Drawbacks with this is that you can't set the filename when it gets saved

like image 95
Nick Beeuwsaert Avatar answered Oct 19 '22 14:10

Nick Beeuwsaert


You cant do this with javascript but you can have a HTML5 link to open save dialogue:

<a href="pageToDownload.html" download>Download</a>

You could add some smarts to automate it on the processed page after the POST.

fiddle : http://jsfiddle.net/ghQ9M/

like image 32
Pogrindis Avatar answered Oct 19 '22 16:10

Pogrindis