javascript - textarea content inside a .txt file but keep line-breaks -


i have piece of code saves value of textarea local text file. seems work fine don't want lose line breaks. code & fiddle:

html

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

js

(function () {     var textfile = null,         maketextfile = function (text) {             var data = new blob([text], {type: 'text/plain'});      // if replacing generated file need     // manually revoke object url avoid memory leaks.     if (textfile !== null) {         window.url.revokeobjecturl(textfile);     }      textfile = window.url.createobjecturl(data);     return textfile;     };      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);  })(); 

http://jsfiddle.net/qm5ag/562/

is there way keep line-breaks? please me out! thanks.

you can use replace

like this:

text = text.replace(/\n/g, "\r\n"); var data = new blob([text], {type: 'text/plain'}); 

see demo


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -