x
 
<!DOCTYPE html>
<html>
<body>
<p>本例使用 HTML DOM 将 "onbeforeprint" 事件分配给 body 元素。</p>
<h1>请尝试打印此文档</h1>
<p><b>提示:</b>键盘快捷键,例如 Ctrl+P,可设置要打印的页面。</p>
<p><b>注释:</b>Safari 和 Opera 不支持 onbeforeprint 事件。</p>
<script>
document.getElementsByTagName("BODY")[0].onbeforeprint = function() {myFunction()};
function myFunction() {
  alert("You are about to print this document!");
}
</script>
</body>
</html>