Reputation: 287
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
Upvotes: 0
Views: 2940
Reputation: 9495
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.
Upvotes: 0
Reputation: 747
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.
Upvotes: 1