0

I have a page with HTML/JavaScript code I want to export to an HTM file when the user presses the export button on the page. I basically just need to find a way to trigger Ctrl+S to execute the Save Page dialog window. I have searched all over and can't seem to find any thing for this that allows JavaScript to simulate that key press sequence.

TL:DR - Does anyone know how to simulate CTRL+S key press in JavaScript/jQuery

Xyz
  • 5,955
  • 5
  • 40
  • 58
Troublesum
  • 257
  • 4
  • 10

3 Answers3

2

Use the saveDocument() method. Docs for it here.

Edit: That only works in Firefox.

austin
  • 5,816
  • 2
  • 32
  • 40
  • I tried to instantiate the class object like its says. var webBrowserPersist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist); and then tried webBrowserPersist.saveDocument() but i get an error: Components.classes is undefined – Troublesum Sep 12 '13 at 20:41
1

I don't think Javascript can do that. There is something for printing but not for saving.

What you can do you create a hint for the browser that the file is an attachment.
You need to send some HTTP headers. You can for example do that with PHP:

header('Content-disposition: attachment');

Maybe .htaccess works also if you don't want to use PHP. You can look that up.

Johannes
  • 737
  • 5
  • 10
  • I think this may be the route i take since its what I use for normal file downloads any... ill update if I get it working.. thanks – Troublesum Sep 12 '13 at 22:22
0

If you want the browser to save the user's page preserving changes in the DOM, this might be beyond the scope of JavaScript, which aims to provide interaction with the page itself, not the environment it's working in.

On some devices this might even be inapplicable - saving pages in Android browsers it not that straightforward and not always possible.

Still, if you're looking just for a working solution for several desktop browsers, you could look at TiddlyWiki, which is a kind of a "local wiki", content on which are kept client-side and saved with the page. Saving is implemented in Java (not JavaScript!) applet distributed with the page. Kind of a web-based browser-based application.

D-side
  • 9,150
  • 3
  • 28
  • 44