我在页面中有这个iframe:document.querySelector("iframe")
它从服务器加载PDF。为了避免在用户点击按钮时调用另一个请求,我想重用iframe和iframe中的pdf文档:ex:<iframe src="myAPI/loadPdf.pdf"><iframe>
当用户单击按钮时:
var pdfWindow = window.open("");
console.log(document.querySelector("iframe"));
pdfWindow.document.write(document.querySelector("iframe"));
//to avoid making 2 request, use the one that is already loaded
这将[object HTMLIFrameElement]
写在空白页而不是文档上
document.write()
在文档中写了一个字符串。您想要将iframe
节点附加到document.body
:
pdfWindow.document.body.appendChild(document.querySelector('iframe'))
但请注意,当您附加iframe时,您可以在网络面板中看到pdf文件的请求,但是如果浏览器可以缓存文件并且缓存版本可用,则使用缓存版本并且不会再次下载。因为它说重用iframe
实际上是没有用的,因为如果你只是打开带有pdf文件的url的窗口就会发生相同的事情,如果它可用,将使用缓存版本。