function download(text, name, type) {_x000D_
var a = document.getElementById("a");_x000D_
var file = new Blob([text], {type: type});_x000D_
a.href = URL.createObjectURL(file);_x000D_
a.download = name;_x000D_
}
_x000D_
<a href="" id="a">click here to download your file</a>_x000D_
<button onclick="download('file text', 'myfilename.txt', 'text/plain')">Create file</button>
_x000D_
And you would then download the file by putting the download attribute on the anchor tag.
The reason I like this better than creating a data url is that you don't have to make a big long url, you can just generate a temporary url.