简体   繁体   English

如何将当前文档的innerHTML下载为文件?

[英]How to download the current document's innerHTML as a file?

Is there a way I could download the current document's innerHTML as file programmatically ? 有没有办法以编程方式将当前文档的innerHTML下载为文件?

I've made the following attempt without success. 我做了以下尝试但没有成功。 It does download the current document's source, however that's not what I am looking for, since I want to preserve any post-load document modifications . 它确实下载了当前文档的源代码,但这不是我想要的,因为我想保留任何后载文档修改

 var save = document.createElement('a');
 save.href = "my location href.attr";
 save.target = '_blank';
 save.download = fileName || 'unknown';

 var event = document.createEvent('Event');
 event.initEvent('click', true, true);
 save.dispatchEvent(event);

Perhaps something like this? 也许是这样的? It's probably not very cross-browser however, but it works in Chrome. 但它可能不是非常跨浏览器,但它适用于Chrome。

 function downloadCurrentDocument() { var base64doc = btoa(unescape(encodeURIComponent(document.documentElement.innerHTML))), a = document.createElement('a'), e = new MouseEvent('click'); a.download = 'doc.html'; a.href = 'data:text/html;base64,' + base64doc; a.dispatchEvent(e); } downloadCurrentDocument(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM