在本文中,我们要执行的任务是在HTML文档打印之前执行一个脚本。
当页面即将打印时,将使用 HTML onbeforeprint 属性。在出现打印对话框之前,会显示警报消息。 onbeforeprint 属性与 onafterprint 属性一起使用。这是事件属性的一部分。
让我们深入以下示例,了解更多关于在HTML中在文档打印之前执行脚本的内容。
在下面的示例中,我们使用了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>
执行脚本时,它将生成一个输出,在网页上显示脚本中使用的文本。
当用户尝试打印网页时,会触发该事件并在网页上显示“确保打印”警报。
这是另一个实现 onbeforeprint 属性的示例。
<!DOCTYPE html> <html> <body onbeforeprint = "display()"> <h1>TUTORIALSPOINT</h1> <script> function display() { alert("The document will now print."); } </script> </body> </html>
运行上述脚本后,输出窗口弹出并在网页上显示文本“tutorialspoint”。
如果用户尝试打印网页,则会触发该事件并在网页上显示警报。