如何在 HTML 中在文档打印前执行脚本?
本文将要执行的任务是在 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”。
如果用户尝试打印网页,事件会被触发并在网页上显示一个警报。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP