gpt4 book ai didi

javascript - 使用 Javascript 打印时防止 Chrome 中的弹出窗口

转载 作者:行者123 更新时间:2023-11-29 15:40:35 27 4
gpt4 key购买 nike

Javascript

function printPage(htmlPage)
{
var w = window.open("about:blank");
w.document.write(htmlPage);
w.print();
}

这里我们打开 w 窗口,然后使用打印对话框打印页面。如您所知,Chrome 浏览器的默认打印对话框右侧已经有一个打印预览。所以,我不想显示窗口 w。是否可以在不打开弹出窗口的情况下直接显示 Chrome 的打印对话框?

最佳答案

正如 hank 所建议的,您可以从 iframe 打印(也可以是隐藏的)。

<html>
<head>
<title>Print in iframe</title>
</head>

<body>

<iframe name="PrintFrame" id="PrintFrame" style="display: none"></iframe>



<script type="text/javascript">

function printPage(htmlPage)
{
window.frames["PrintFrame"].document.write(htmlPage);
window.frames["PrintFrame"].print();

}
printPage('<html><head><title></title></head><body><table border="1"><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table></body></html>');

</script>
</body>
</html>

关于javascript - 使用 Javascript 打印时防止 Chrome 中的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722473/

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