JavaScript Window print() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Print dialog

What You’ll Learn

The print() method opens the browser’s print dialog for the current page. This tutorial covers syntax, print buttons, @media print CSS, beforeprint / afterprint events, five examples, and what JavaScript cannot do (force silent printing).

01

Syntax

window.print()

02

Dialog

User confirms print

03

Button

Click to trigger

04

CSS

@media print

05

Events

before / after print

06

Return

undefined

Introduction

Invoices, receipts, articles, and reports often need a paper or PDF copy. Browsers already expose printing through the menu or Ctrl+P (Cmd+P on Mac). The window.print() method lets your page offer the same flow from a custom Print button.

Calling print() does not send pages to the printer immediately—it opens the system print dialog where the user chooses printer, pages, and orientation. Your job is to trigger it at the right moment and use CSS so the printed layout looks clean.

Understanding the print() Method

window.print() takes no arguments and returns undefined. It applies to the current browsing context—the visible document in that tab (or in a popup if you call popupWindow.print() on a reference you opened).

Because printing affects the whole window, pair JavaScript with @media print styles to hide menus, sidebars, and buttons that should not appear on paper. The screen view stays the same; only the print preview uses those rules.

💡
Beginner Tip

print() and window.print() are the same in browsers. Always wire printing to a button—never call it automatically when the page loads.

📝 Syntax

General form of Window.print():

JavaScript
window.print();
// shorthand (same in browsers):
print();

Parameters

  • None.

Return value

  • undefined.

Related APIs

  • window.addEventListener("beforeprint", fn) — dialog opening.
  • window.addEventListener("afterprint", fn) — dialog closed.
  • @media print { ... } in CSS — print-only layout.
  • iframe.contentWindow.print() — print iframe document (same-origin).

⚡ Quick Reference

TopicDetail
Open print dialogwindow.print()
From buttonbtn.onclick = () => print()
Hide UI on paper@media print { .no-print { display: none } }
Before dialogbeforeprint event
After dialogafterprint event
Skip dialog?No — user must confirm

Examples Gallery

Use the Try-it links to open the print dialog from a button. Preview print layout with the browser’s print preview (Ctrl+P or the dialog’s preview pane).

📚 Getting Started

Call print() from a function.

Example 1 — Basic print() Call

Wrap the method in a function you can attach to UI later.

JavaScript
function printPage() {
  window.print();
}

// User clicks Print → print dialog opens
printPage();
Try It Yourself

How It Works

The method is synchronous in the sense that it opens the dialog immediately; the user’s print or cancel choice happens afterward in the browser UI.

📈 Practical Patterns

Buttons, print CSS, events, and focused content areas.

Example 2 — Print Button with addEventListener

Connect a button to print()—the standard production pattern.

JavaScript
document.getElementById("printBtn").addEventListener("click", function () {
  window.print();
});
Try It Yourself

How It Works

User-initiated printing is expected UX. Label the button clearly (“Print” or “Save as PDF”) so users know what will happen.

Example 3 — Hide Chrome with @media print

Keep navigation and buttons on screen but remove them from print output.

JavaScript
@media print {
  .no-print {
    display: none !important;
  }

  body {
    font-size: 12pt;
    color: #000;
  }
}
Try It Yourself

How It Works

Add class no-print to elements users should not see on paper. Test with print preview in DevTools (Rendering → emulate CSS media type print).

Example 4 — Listen for beforeprint and afterprint

Run setup or cleanup when the print dialog opens and closes.

JavaScript
window.addEventListener("beforeprint", function () {
  console.log("Print dialog opened — expand hidden details if needed.");
});

window.addEventListener("afterprint", function () {
  console.log("Print dialog closed.");
});
Try It Yourself

How It Works

Some apps temporarily show extra rows or swap charts to high-contrast mode in beforeprint, then revert in afterprint.

Example 5 — Printable Receipt Section

Mark printable content and hide everything else on paper.

JavaScript
<nav class="no-print">Site navigation</nav>

<section class="receipt">
  <h1>Order #1042</h1>
  <p>Total: $24.99</p>
</section>

<button type="button" class="no-print" onclick="window.print()">Print receipt</button>

<style>
  @media print {
    .no-print { display: none; }
  }
</style>
Try It Yourself

How It Works

E-commerce and booking sites combine a Print button with print-only CSS so receipts look professional without duplicate HTML pages.

🚀 Common Use Cases

  • Print this page — articles, documentation, and tutorials.
  • Receipts and invoices — order confirmations with print-friendly CSS.
  • Tickets and passes — event or boarding details users can print or save as PDF.
  • Reports — dashboards with print styles that simplify charts for paper.
  • Maps and directions — printable summary of route details.
  • Popup print preview — open content in window.open(), then popup.print().

🧠 What Happens When You Call print()

1

User or script

Button click calls window.print().

Trigger
2

beforeprint

Browser fires beforeprint; @media print styles apply.

Event
3

Print dialog

User picks printer, pages, orientation, or Save as PDF.

UI
4

afterprint

Dialog closes; afterprint runs. Method returns undefined.

📝 Notes

  • JavaScript cannot print silently without the dialog—users must confirm.
  • Background colors may not print unless users enable “background graphics” in the dialog.
  • Test print layout in Chrome DevTools → Rendering → print media type.
  • Long pages: use CSS page-break-inside: avoid on cards and tables where needed.
  • Mobile browsers support print() but may route to share/save sheets instead of desktop printers.
  • For a dedicated print view, open a minimal popup with window.open() and call print() there.

Browser Support

Window.print() is supported in every major browser. The print dialog appearance and “Save as PDF” option depend on the operating system.

Universal · Standard API

Window.print()

Supported in Chrome, Firefox, Safari, Edge, Opera, and mobile browsers. Pair with @media print for consistent output.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
print() Universal

Bottom line: Safe to use anywhere users need a Print button. Always combine with print-specific CSS for professional output.

Conclusion

window.print() opens the browser print dialog for the current page. It is simple to call but works best with thoughtful UX—a clear Print button and CSS tuned for paper.

Use @media print to hide chrome, listen for beforeprint / afterprint when you need hooks, and remember that the user always confirms printing in the dialog.

💡 Best Practices

✅ Do

  • Trigger print() from a Print button click
  • Add @media print styles for clean output
  • Hide nav, ads, and buttons with .no-print
  • Test print preview before shipping
  • Use beforeprint for last-moment layout tweaks

❌ Don’t

  • Call print() automatically on page load
  • Expect silent printing without user confirmation
  • Assume screen colors and layout match on paper
  • Forget mobile print/share behavior differs
  • Rely on JavaScript alone without print CSS

Key Takeaways

Knowledge Unlocked

Five things to remember about print()

Your foundation for printing web pages from JavaScript.

5
Core concepts
🖨 02

Dialog

User confirms.

UX
🎨 03

@media print

Paper layout.

CSS
🔄 04

Events

before / after.

Hooks
🖱 05

Button

Click to print.

Pattern

❓ Frequently Asked Questions

It opens the browser's print dialog so the user can print the current page (or save it as PDF). JavaScript cannot skip the dialog or force printing without user confirmation.
No. Call window.print() with no parameters. It returns undefined.
No. Trigger print from a user action such as a Print button click. Unexpected print dialogs frustrate users and may be blocked in some environments.
Use CSS @media print rules—for example .no-print { display: none; } on headers and buttons. Screen styles stay unchanged; print layout applies only in the print preview.
Yes. The beforeprint event fires when the user opens the print dialog; afterprint fires when it closes. Listen with window.addEventListener('beforeprint', handler).
Use @media print to hide unwanted sections, or open a popup/iframe with the content and call print() on that window. There is no parameter to print a single DOM node directly.
Did you know?

Many users “print” web pages to PDF using the print dialog’s Save as PDF option—so good @media print styles improve both paper and PDF exports from the same print() call.

Continue to prompt()

Learn how to show a text input dialog with the window.prompt() method.

prompt() 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.

6 people found this page helpful