W3C home > Mailing lists > Public > whatwg@whatwg.org > December 2009

[whatwg] window.print() when printing is not supported

From: Jonas Sicking <jonas@sicking.cc>
Date: Mon, 28 Dec 2009 23:04:27 +0100
Message-ID: <63df84f0912281404o6d734d5ctd887ccaad23af56e@mail.gmail.com>
On Mon, Dec 28, 2009 at 10:14 PM, Markus Ernst <derernst at gmx.ch> wrote:
> Michael A. Puls II schrieb:
>>
>> On Mon, 28 Dec 2009 14:39:06 -0500, Michael A. Puls II
>> <shadow2531 at gmail.com> wrote:
>>
>>> On Mon, 28 Dec 2009 12:08:48 -0500, Boris Zbarsky <bzbarsky at mit.edu>
>>> wrote:
>>>
>>>> On 12/28/09 9:54 AM, Michael A. Puls II wrote:
>>>>>
>>>>> As for window.print(), what if it returns false if printing is not
>>>>> supported?
>>>>
>>>> It might be better as |true| (crappy API issues aside), because
>>>> otherwise if a page writes |if (window.print())| it'll get false in all
>>>> existing browsers, no?
>>>
>>> Ah, I see. Good point.
>>
>> Since making print() return something isn't going to help with older UAs
>> and returning true would feel backwards, instead of:
>>
>> if (window.print()) {
>> ? ?alert("Printing is not supported");
>> }
>>
>> , we could do this:
>>
>> if (typeof navigator.printingEnabled === "function" &&
>> !navigator.printingEnabled()) {
>> ? ?alert("Printing is not supported");
>> } else {
>> ? ?window.print();
>> }
>>
>> It's more verbose though and I'm not sure any will like it. But, tossing
>> it out there.
>>
>
> What about:
>
> if (!window.print) {
> ? ?alert("Printing is not supported"); [1]
> } else {
> ? ?window.print();
> }
>
> This is possible if - one of Ollis initial proposals - browsers which don't
> support window.print() do not have the print method at all. That is
> backwards compatible, as legacy Handheld, TV and whatever UAs, that may have
> a however-working print method, just behave the same way they always did.
>
> [1] I'd actually prefer something like:
> body.onload = function() {
> ? ?if (window.print) {
> ? ? ? ?// Code that creates the print button
> ? ? ? ?// and adds it to the appropriate parent element
> ? ?}
> }

The problem with this is that it'll break existing pages that simply do:

doStuff;
window.print();
doMoreStuff;

since the 'doMoreStuff' code will never run since the window.print
line will throw due to trying to call a function that does not exist.

/ Jonas
Received on Monday, 28 December 2009 14:04:27 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:54 UTC