IE only solution:
function SaveContents(element) {
if (typeof element == "string")
element = document.getElementById(element);
if (element) {
if (document.execCommand) {
var oWin = window.open("about:blank", "_blank");
oWin.document.write(element.value);
oWin.document.close();
var success = oWin.document.execCommand('SaveAs', true, element.id)
oWin.close();
if (!success)
alert("Sorry, your browser does not support this feature");
}
}
}
Required HTML sample:
<textarea id="myText"></textarea><br />
<button type="button" onclick="SaveContents('myText');">Save</button>
This will save the contents of the given textarea into a file with name equal to the ID of the textarea.
As for other browsers, you can read this: Does execCommand SaveAs work in Firefox?
Test case and working example: http://jsfiddle.net/YhdSC/1/ (IE only..)
NOTE: https://support.microsoft.com/en-us/help/281119/internet-explorer-saves-html-content-instead-of-the-active-document
It may not work for filetypes other than txt