Save HTML locally with Javascript

后端 未结 10 1559
夕颜
夕颜 2020-12-03 01:19

I do know that Javascript cannot write data in the filesystem, for security reasons. I have often read that the only way to save data locally with Javascript is cookies or <

10条回答
  •  感情败类
    2020-12-03 01:56

    You can just use the Blob function:

    function save() {
      var htmlContent = ["your-content-here"];
      var bl = new Blob(htmlContent, {type: "text/html"});
      var a = document.createElement("a");
      a.href = URL.createObjectURL(bl);
      a.download = "your-download-name-here.html";
      a.hidden = true;
      document.body.appendChild(a);
      a.innerHTML = "something random - nobody will see this, it doesn't matter what you put here";
      a.click();
    }
    

    and your file will save.

提交回复
热议问题