Creating a downloadable text-file using blobs

Created as part of my answer to http://stackoverflow.com/questions/8178825/create-text-file-in-javascript

by Gibrain Hernandez

HTML

<textarea id="textbox">Type something here</textarea> <button id="create">Create file</button> <a download="info.txt" id="downloadlink" style="display: none">Download</a>

JavaScript

(function () {
var textFile2="";
var textFile="";
  makeTextFile = function (text) {
  textFile +=text+console.trace()+"\r\n";
  console.log("textFile",textFile);
  
    var data = new Blob([textFile], {type: 'text/plain'});
console.log("enter the function");
    // If we are replacing a previously generated file we need to
    // manually revoke the object URL to avoid memory leaks.
    if (textFile !== null) {
      window.URL.revokeObjectURL(null);
    }

    textFile2 = window.URL.createObjectURL(data);

    return textFile2;
  };


  var create = document.getElementById('create'),
    textbox = document.getElementById('textbox');

  create.addEventListener('click', function () {
    var link = document.getElementById('downloadlink');
    link.href = makeTextFile(textbox.value);
    link.style.display = 'block';
  }, false);
})();