February 15, 2006
UPDATE: This article was written over a decade ago. I haven’t tested whether this technique still works in Firefox (or any other browser with a similar setting). Use at your own risk. If you truly need this functionality, you’ll need to find out which version of Firefox was available back in early 2006 and follow these instructions.
Imagine the horrific screams of Internet users world-wide as their printers start spewing coupons and ads from their printer without warning, and without the precious print dialog box to “cancel” the print process.
As it turns out, using window.print()
in JavaScript can open a print dialog box for a given web page. All
a user needs to do is click the OK
button and their printer will spit out a copy of that web page content.
Web browsers use this dialog box ‘halt’ to protect users from silently printing things in the background simply by visiting a web page. But what if you have a legitimate use-case, such as running a web-based Point-Of-Sale system through a browser, and you really do need to automatically print something (like a receipt) when a page contains a window.print() call?
In Firefox, clear out the address bar if it’s not already blank, and type this where you’d otherwise type in a URL like www.google.com:
about:config
This will bring up all kinds of configuration settings about the browser.
By default, Firefox wants to protect you SO badly from automated printing, that they don’t even include an option here for you to toggle. After browsing the source code for Firefox over my lunch hour (hey, a geek’s gotta feed his mind too), I found a setting that will do the trick – it shuts off the print dialog box, and will auto-magically print a page containing a window.print() javascript call.
On the page with all of these configuration options, right-click on the list of options, select New
and
choose Boolean
– this is important, you MUST choose a Boolean type, not string or integer. The name of
the setting is this:
print.always_print_silent
When you click the OK button, Firefox will prompt you to select the true or false value. Select true
.
The modification gets written into your preferences file immediately, and now every page you visit with
a window.print() call will spew paper out of your printer like a baby who doesn’t like mushed spinach.
To turn the feature back off, you can just double-click the new setting in the about:config
list.
If you can’t find it, just use the “Filter:” box, and type the word silent
as the search option and
hit Enter, and that should locate it.
I’ve tested this on Ubuntu/Debian and Firefox 1.0.7. Feel free to comment back on this if you’ve tested this on other OS’s or versions of Firefox. You can even build an auto-printing page to see it in action:
<html>
<head>
<title>Ian was here</title>
</head>
<body>
This is my auto-printing page!
</body>
<script type="text/javascript">
window.print() ;
</script>
</html>
Other things worth noting:
- If you misspell the new option name, I don’t believe Firefox can delete the key for you – you may need to
manually (and carefully) edit your preferences file in a text editor. The file you’ll edit is called
prefs.js
- Likewise, if you don’t choose a Boolean value (I tried a ‘string' value first), you must also manually (and carefully) modify your preferences file.
- Make sure you have a working printer set up and available to Firefox, otherwise this won’t work at all. You may get other print configuration warnings or no printer output.