gpt4 book ai didi

html - 下载页面 HTML 的按钮

转载 作者:行者123 更新时间:2023-11-27 22:48:53 27 4
gpt4 key购买 nike

有没有办法让按钮/链接在您单击它时,它会获取当前页面位置并下载它的 HTML 版本?它也将是一个 iframe,链接应该只是下载 iframe 的内容。谢谢!

最佳答案

以下 JavaScript 将获取当前文档并将其作为下载链接提供。在 Chrome 中测试过,不确定其他人。请记住,IE 对 DataURI 大小有限制。此外,您将丢失外部图像/CSS/等,除非您将 base 标记插入 head 标记的顶部(或找到其他方法来滚动)资源):

// create the link to trigger download
// you could alternatively fetch an existing tag and update it
var a = document.createElement('a');

// send as type application/octet-stream to force download, not in browser
a.href =
"data:application/octet-stream;base64," +
btoa("<html>"+ document.getElementsByTagName('html')[0].outerHTML +
"</html>");

a.innerText = "Download this page";

// put the link wherever you want
document.body.appendChild(a);

编辑:也没有在下载链接的末尾提供文件名或 .htm...哼。这些东西只能由 Content-Disposition header 真正提供,并且需要将请求发送到服务器,所以...不是很棒的用户体验,但却是获得准确的最简单方法用户看到的页面状态。

关于html - 下载页面 HTML 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5333893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com