HTML onbeforeprint Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onbeforeprint attribute handles the window beforeprint event. It runs when the document is about to be printed or opened in print preview.

Put it on <body> (window handlers on body listen on window), or use window.addEventListener("beforeprint", …). Pair with onafterprint to restore the screen view afterward. Prefer @media print for styling.

Baseline · Widely available.

Stable across modern browsers since about September 2019. Not deprecated.

Inline handlers discouraged.

MDN: prefer CSS for print layout, and addEventListener over inline on* attributes when script is required.

HTML Syntax

syntax.html
<body onbeforeprint=" script">

script runs when beforeprint fires. Preferred: window.addEventListener("beforeprint", handler).

Attribute Values

How you listen for beforeprint:

Value / formDescriptionTry it
JavaScript on bodyInline handler, e.g. preparePrint()Try it
addEventListener("beforeprint")Preferred modern listener on windowTry it
With afterprintPrepare for print, then restore afterwardTry it

Live Preview

Open the system print dialog, or simulate beforeprint without printing. Status updates when the event fires.

action

Print opens the browser dialog. Simulate dispatches beforeprint for study when you cannot print.

Demo

Waiting for beforeprint…

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Prefer listeners + CSS print rules in real apps.

Example 1 — Inline onbeforeprint on body

Recognize the attribute form. Open print to see the message as preview starts.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>onbeforeprint demo</title>
</head>
<body onbeforeprint="document.getElementById('status').textContent = 'beforeprint fired.'">
  <p id="status">Open print to trigger beforeprint.</p>
  <button type="button" onclick="window.print()">Print…</button>
</body>
</html>
Try It Yourself

How It Works

  1. onbeforeprint on body registers a window-level handler.
  2. window.print() opens the browser print UI.
  3. beforeprint runs as print/preview is about to start.
  4. Status text confirms the handler ran.

Example 2 — Preferred addEventListener

Same event, cleaner separation of HTML and script.

HTML
<p id="status">Waiting for beforeprint…</p>
<button type="button" id="printBtn">Print…</button>

<script>
  var status = document.getElementById("status");

  window.addEventListener("beforeprint", function () {
    status.textContent = "beforeprint fired.";
  });

  document.getElementById("printBtn").addEventListener("click", function () {
    window.print();
  });
</script>
Try It Yourself

How It Works

  1. No attribute — the listener is attached on window.
  2. Same beforeprint signal as the inline form.
  3. Easier to maintain — no markup/script mix.
  4. IDL option: window.onbeforeprint = handler also works (replaces prior property handlers).

Example 3 — beforeprint + afterprint

Hide a banner for print, then show it again afterward.

HTML
<div id="banner">Screen-only promo</div>
<p id="status">Idle</p>
<button type="button" id="printBtn">Print…</button>

<script>
  var banner = document.getElementById("banner");
  var status = document.getElementById("status");

  window.addEventListener("beforeprint", function () {
    banner.hidden = true;
    status.textContent = "beforeprint — banner hidden.";
  });

  window.addEventListener("afterprint", function () {
    banner.hidden = false;
    status.textContent = "afterprint — banner restored.";
  });

  document.getElementById("printBtn").addEventListener("click", function () {
    window.print();
  });
</script>
Try It Yourself

How It Works

  1. beforeprint prepares the page (hide promo).
  2. afterprint restores the screen layout.
  3. CSS first: for simple hide/show, @media print is often enough.
  4. Use JS when print needs temporary DOM changes CSS cannot cover.

Edge Cases & Pitfalls

Most issues come from using JS for styling that CSS print media can already handle.

css first

Prefer @media print

MDN recommends print CSS for layout. Use beforeprint when you must mutate the DOM before printing.

window

Window-level event

Listen on window (or body for the HTML attribute). Putting it on a random div will not help.

restore

Always pair with afterprint

If you hide or rewrite content in beforeprint, restore it in afterprint — including when the user cancels preview.

style

Prefer listeners

Inline on* attributes mix concerns. Use addEventListener in production code.

timing

Keep prepare work fast

Heavy work in beforeprint can delay the print UI. Prefer light toggles; do heavy prep earlier if possible.

Browser Support

onbeforeprint is an HTML / window event handler for the beforeprint event. Prefer addEventListener and @media print in apps.

✓ Baseline · Widely available

onbeforeprint

Widely available since about September 2019. Chrome, Firefox, Safari, Edge, and Opera support beforeprint on window.

95% Global support (approx.)
Google Chrome 63+
Full support
Mozilla Firefox 6+
Full support
Apple Safari 13+
Full support
Microsoft Edge 12+
Full support
Opera 50+
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy*
Supported*
beforeprint Widely supported

Bottom line: *Safe on modern browsers. Prefer @media print for styling; use beforeprint to prepare JS-driven print tweaks.

Key Takeaways

  • Role: run script as print/preview is about to start.
  • Pair with: afterprint to restore the screen view.
  • Prefer: @media print + addEventListener.
  • Target: window / body, not arbitrary elements.

One line: onbeforeprint prepares the page for print/preview — restore with afterprint, style with CSS when you can.

Frequently Asked Questions

It is an event handler for the beforeprint event. That event fires when the document is about to be printed or opened in print preview. Pages often use it to prepare content (for example hide a banner) before printing.
It is a window-level handler. In HTML, put it on body (window event handlers on body listen on window), or use window.addEventListener("beforeprint", …) / window.onbeforeprint.
Prefer CSS @media print for print styling. Use beforeprint/afterprint only when you must change the DOM with JavaScript around printing.
beforeprint runs as print/preview is about to start (prepare content). afterprint runs afterward (restore content). Use them as a pair when JS changes are required.
Prefer window.addEventListener("beforeprint", handler). MDN discourages inline HTML event handler attributes.
Yes. MDN: beforeprint fires when the document is about to be printed or previewed for printing.

Next: onbeforeunload

Warn users before they leave a page with unsaved work.

onbeforeunload tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful