--- title: beforeprint slug: Web/API/Window/beforeprint_event translation_of: Web/API/Window/beforeprint_event original_slug: Web/Events/beforeprint ---

当相关联的文档即将打印或预览以进行打印时,将触发beforeprint事件。

基本信息

Specification
HTML5
Interface
Event
Bubbles
No
Cancelable
No
Target
DefaultView (<window>)
Default Action
None

属性

属性 类型 描述
target {{readonlyInline}} {{domxref("EventTarget")}} 事件目标 (DOM 树中的最顶层目标)
type {{readonlyInline}} {{domxref("DOMString")}} 时间类型
bubbles {{readonlyInline}} {{jsxref("Boolean")}} 事件是否冒泡
cancelable {{readonlyInline}} {{jsxref("Boolean")}} 事件是否可取消

样例

使用 addEventListener()

window.addEventListener('beforeprint', (event) => {
  console.log('Before print');
});

使用 onbeforeprint 事件监听属性:

window.onbeforeprint = (event) => {
  console.log('Before print');
};

规范

Specification Status
{{SpecName('HTML WHATWG', '#event-beforeprint')}} {{Spec2('HTML WHATWG')}}

浏览器兼容性

{{Compat("api.Window.beforeprint_event")}}

相关事件