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
Fundamentals
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.
Concept
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.
Foundation
📝 Syntax
General form of Window.print():
JavaScript
window.print();
// shorthand (same in browsers):
print();
JavaScript opens the same dialog as Ctrl+P; CSS shapes what appears in print preview.
print()
window.print()
Custom Print button
Ctrl+P
Browser shortcut
Same dialog, no JS
@media print
print styles
Layout on paper
PDF save
Print → PDF
User choice in dialog
Hands-On
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();
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Wrap Up
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.
Your foundation for printing web pages from JavaScript.
5
Core concepts
📝01
Syntax
window.print()
API
🖨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.