1(function () {
2 var textFile = null;
3 function makeTextFile(text) {
4 var data = new Blob([text], {type: 'text/plain'});
5
6 // If we are replacing a previously generated file we need to
7 // manually revoke the object URL to avoid memory leaks.
8 if (textFile !== null) {
9 window.URL.revokeObjectURL(textFile);
10 }
11
12 textFile = window.URL.createObjectURL(data);
13
14 return textFile;
15 }
16
17
18 var create = document.getElementById('create');
19 var textbox = document.getElementById('textbox');
20
21 //create a click event listener
22 create.addEventListener('click', function () {
23 var link = document.getElementById('downloadlink');
24 link.setAttribute('download', 'info.txt');
25 //make the text file
26 link.href = makeTextFile(textbox.value);
27 link.style.display = 'block';
28 //wait for the link to be rendered and then initiate a click to download the file
29 window.requestAnimationFrame(function () {
30 var event = new MouseEvent('click');
31 link.dispatchEvent(event);
32 document.body.removeChild(link);
33 });
34 }, false);
35
36})();