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

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