Skip to content

Instantly share code, notes, and snippets.

@cw
Last active March 7, 2017 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cw/4383852 to your computer and use it in GitHub Desktop.
Save cw/4383852 to your computer and use it in GitHub Desktop.
App = function();
App.prototype.write_file = function(filename, code) {
var elements, elem, evt;
// remove existing WriteFileElem
elements = document.getElementsByTagName('WriteFileElem');
while (elements[0]) elements[0].parentNode.removeChild(elements[0]);
// make new WriteFileElem and hide it
elem = document.createElement("WriteFileElem");
elem.setAttribute("filename", filename);
elem.setAttribute("encoding", "ascii");
elem.setAttribute("style", "display:none");
// set content to passed `code` string
elem.innerHTML = code;
document.documentElement.appendChild(elem);
// trigger WriteFile event
evt = document.createEvent("Events");
evt.initEvent("WriteFile", true, false);
return elem.dispatchEvent(evt);
};
<html>
<body>
<WriteFileElem filename="/Users/joe/Desktop/writefile.txt" encoding="ascii">
innerHTML of this element will be written to filename attribute value, if that file *doesn't exist*.
</WriteFileElem>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment