Reputation: 3682
I have a button.Now when I click on the button the cltrl+s will be pressed or tiggered.How to do that/ I have tried,
$( "#btn" ).click(function() {
var e = jQuery.Event( "keydown", { keyCode: 83, ctrlKey:true} );
jQuery("body").trigger( e );
});
Upvotes: 0
Views: 1390
Reputation: 199
Use an <a href>
(html5) like:
<a href="downloads/file.xml" download>Download File</a>
And if you like to create an XML file in javascript and then save it by an a tag you can do:
var xml = '<xml><node1>bla</node1><node2 var="aa">abla</node2></xml>';
xml = btoa(xml); // Converts xml string to base64 (use atob to convert base64 back to string)
var a = document.createElement('a'); // Create A element
a.href = 'data:text/xml;base64,' + xml; // Set base64 encoded href
a.setAttribute('download', ''); // add html5 download tag
a.appendChild(document.createTextNode('download xml file'); // Add link text
body.appendChild(a); // And add element to the page.
Paste this code in a html file and see for your self :)
Note: this code might not work in all browsers, tested with IE11 and Chrome
Upvotes: 1