最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

So I've been researching this for a couple days and haven't e 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: /

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.

So I've been researching this for a couple days and haven't e 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/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.

Share Improve this question edited Jan 8, 2014 at 0:59 user2672373 asked Jan 8, 2014 at 0:54 LukeLCLukeLC 731 gold badge1 silver badge3 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

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

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/ghQ9M/

Simple answer, you can't. JavaScript is restricted to perform such operations due to security reasons.

The best way to acplish that, would be, to call a server page that would write the new file on the server. Then from javascript perform a POST request to the server page passing the data you want to write to the new file.

If you want the user to save the page to it's file system, this is a different problem and the best approach to acplish that, would be to, notify the user/ask him to save the page, that page could be your new window like you are doing w.open().

Let me do some demonstration for you:

   //assuming you know jquery or are willing to use it :)
   var html = $("#tblive").html().replace(/</g, '&lt;');

   //generating your download button
   $.post('generate_page.php', { content: html })
      .done(function( data ) {
          var filename = data;

          //inject some html to allow user to navigate to the new page (example)
          $('#tblive').parent().append(
              '<a href="'+filename+'">Check your Dynamic Page!</a>');

          // you data here, is the response from the server so you can return
          // your new dynamic page file name here.
          // and maybe to some window.location="new page";  
      });

On the server side, something like this:

<?php 
   if($_REQUEST["content"]){
      $pagename = uniqid("page_", true) . '.html';
      file_put_contents($pagename, $_REQUEST["content"]);
      echo $pagename;
   }
?>

Some notes, I haven't tested the example, but it works in theory. I assume that with this the effort to implement it should be minimal, assuming this solves your problem.

A server based solution:

You'll need to set up a server (or your PC) to serve your HTML page with headers that tell your browser to download the page instead of processing the HTML markup. If you want to do this on your local machine, you can use software such as WAMP (or MAMP for Mac or LAMP for Linux) that is basically a web server in a .exe. It's a lot of hassle but it'll work.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论