因此,我试图建立一个加载某些内容的新窗口(这是因为该内容位于iframe中,而chrome在尝试打印时没有显示该iframe中的任何内容)。该新窗口允许用户打印,然后关闭。但是,在较慢的浏览器上,在装入打印盒之前会触发关闭功能,从而导致窗口关闭。我有办法在开门前检查打印盒以防止这种情况吗?
// Check if the browser is chrome. If it is, load the iframe into a new window to print
// as chrome doesnt allow for iframes to be printed within a page. if not, just print the page.
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (isChrome) {
// Define iframe containing heatmap and a new window to print it in
var iframe = document.getElementById("heatmap");
var printWindow = window.open("","printwindow");
// Create css link for print window
var css = document.styleSheets[2].href;
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = css;
// Create print window
printWindow.document.write("<html><head><title>Heatmap</title></head><body></body></html>");
printWindow.document.head.appendChild(link);
printWindow.document.body.appendChild(iframe);
// Print window and close after 3s to allow browser time to load iframe contents.
// Then reload the page to remove issues from duplicated ids.
setTimeout(function(){
printWindow.print();
printWindow.close();
window.location.reload(true);
}, 3000);
} else {
window.print();
return false;
}