jQuery | Download Textarea Content as Html File
/* Download Textarea Content as Html File.js */
$('#download-button').click(function() {
if ('Blob' in window) {
var fileName = prompt('Please enter file name to save', 'Untitled.html');
if (fileName) {
var textValue = $('textarea').html();
var textToWrite = htmlDecode(textValue);
var textFileAsBlob = new Blob([textToWrite], {
type: 'application/xhtml+xml'
});
if ('msSaveOrOpenBlob' in navigator) {
navigator.msSaveOrOpenBlob(textFileAsBlob, fileName);
} else {
var downloadLink = document.createElement('a');
downloadLink.download = fileName;
downloadLink.innerHTML = 'Download File';
if ('webkitURL' in window) {
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
} else {
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.click(function() {
document.body.removeChild(event.target);
});
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
}
} else {
alert('Your browser does not support the HTML5 Blob.');
}
});