1

I have been hunting around on these forums to find some code which can download the HTML page AS IS (i.e. with added elements from other buttons, and essentially everything within my div's) so that it can be picked up another time with the data still there. I cannot for the life of me find anything which just downloads the page. Below is the closest I came to actually saving a html document at all, and even that (obviously) only displays the text written. Could I edit this to make it encapsulate the entire page? With very little experience I can only apologize.

function save() {
  var anchor = document.querySelectory('button');
  anchor.setAttribute('download', 'index.html');
  anchor.setAttribute('href', 'data:text/html;charset=UTF-8,<p>asdf</p>')
}

2 Answers2

0

One solution is FileSaver.js

Follow the example on that page, and use document.innerHTML for your blob (what I've used) Actually, I forget if that excludes the header. If so, there's a solution I found to this (also on StackOverflow) but I don't have the code here locally to look at how I got around this (if I did, indeed, have to get around this)

Community
  • 1
  • 1
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

You can try this

<a href ="#" id="donwload-link" onClick="myFunction()">download html content</a>

<script>
function myFunction() {
  var content = document.documentElement.innerHTML;
  download(content, "index", "txt")

}
function download(content, fileName, fileType) {
  var link = document.getElementById("donwload-link");
  var file = new Blob([content], {type: fileType});
  var donwloadFile = fileName + "." + fileType;
  link.href = URL.createObjectURL(file);
  link.download = donwloadFile
}
</script>

https://jsfiddle.net/a9oLw1zv/13/