Reputation: 307
I want to refer to a variable that is a long string as a example.txt file I was wondering how I would go about doing this using Javascript. Since I cant save the variable as a .txt file, I was wondering if there was some way using javascript to refer to the variable which is a string as a .txt file.
Upvotes: 0
Views: 37
Reputation: 109
Use Blob and objectUrl
//cross browser
window.URL = (
window.URL ||
window.webkitURL ||
window.mozURL ||
window.msURL
);
var example = new Blob([variable], {type: 'text/plain'}),
url = window.URL.createObjectURL(example);
then basically you can refer to this variable using url or write this url to anchor and download it
document.body.innerHTML += '<a id="captislink" href="'+ url +'" download="example.txt">Save</a>';
Upvotes: 1