在以 HTML 格式打印文档之前执行脚本?
htmlweb developmentfront end technology更新于 2024/8/13 2:20:17
本文中我们将要执行的任务是在以 HTML 格式打印文档之前执行脚本。
当页面即将打印时,将使用 HTML 的 onbeforeprint 属性。在出现打印对话框之前,会显示警告消息。onbeforeprint 属性与 onafterprint 属性一起使用。它是事件属性的一部分。
让我们深入研究以下示例,以进一步了解如何在以 HTML 格式打印文档之前执行脚本。
示例 1
在以下示例中,我们使用 HTML 的 onbeforeprint 属性。
<!DOCTYPE html>
<html>
<body onbeforeprint="mytutorial()">
<h1>MS DHONI</h1>
<h2>Finishes off in his Style.</h2>
<script>
function mytutorial() {
alert("Sure To Print!");
}
</script>
</body>
</html>
脚本执行时,将生成一个输出,在网页上显示脚本中使用的文本。
当用户尝试打印网页时,将触发该事件并在网页上显示"确定打印"的警告。
示例 2
这是另一个实现 onbeforeprint 属性的示例。
<!DOCTYPE html>
<html>
<body onbeforeprint = "display()">
<h1>TUTORIALSPOINT</h1>
<script>
function display() {
alert("The document will now print.");
}
</script>
</body>
</html>
运行上述脚本后,输出窗口会弹出并在网页上显示文本"tutorialspoint"。
如果用户尝试打印网页,则会触发该事件并在网页上显示警报。
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

