How to create a file and generate a download with Javascript in the Browser (without a server)
by Kyle Stay
HTML
Paste link to be redirected:<br>
<textarea id="text-val" rows="4"></textarea><br/>
<input type="button" id="dwn-btn" value="Download dinamically generated html redirect file"/>
JavaScript
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
document.getElementById("dwn-btn").addEventListener("click", function(){
// Generate download of hello.txt file with some content
var text = `<head>
<meta http-equiv="refresh" content="0; URL=${document.getElementById("text-val").value}" />
</head>`
var filename = "redirect.html";
download(filename, text);
}, false);