Save JS File in JavaScript
by Faisal Khan Janjua
HTML
<button id="dl">Create file</button>
JavaScript
const data = `const formCollection = [
{ name: 'Contact form' },
{ name: 'Registration form' },
{ name: 'List form' }
]
export default formCollection;`;
$(document).on('click', '#dl', function(){
const aTag = document.createElement("a");
var file = new Blob([data], {type: 'text/plain'});
// var file = new Blob([data], {type: 'application/json'});
aTag.href = URL.createObjectURL(file);
aTag.download = "myFile.js";
// aTag.download = "myFile.json";
aTag.style = "display: none;";
document.body.appendChild(aTag);
aTag.click();
});