2

I need to know if the user has click the print or cancel button but onafterprint event fires if either was clicked, why ??

window.onafterprint = () => {

    // event should fire only if print button is clicked

}

Enter this page and do the Ctrl + p try to cancel or print or save the document and watch the alert

can anyone please explain ?

user12163165
  • 555
  • 8
  • 12

2 Answers2

0

The afterprint event is fired after the associated document has started printing or the print preview has been closed. MDN

It seems that you can't know through the event. The specification does not tell user agents to distinguish between printing or canceling the print prompt. Although the spec is quite vague about what to actually do. WHATWG Spec.

Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32
  • is there any other way to know that or do I need to develop an application in C-sharp? – user12163165 Aug 31 '20 at 21:37
  • I'm afraid there is no known solution. – Emiel Zuurbier Aug 31 '20 at 21:50
  • 1
    so how people who develop WEB APPLICATIONS can be sure that their users are actually printing an invoice or any other document with DYNAMIC data ?? someone must know the answer to that. – user12163165 Aug 31 '20 at 21:55
  • 1
    See comment above. Also see [this](https://stackoverflow.com/questions/32685957/detect-if-window-print-is-confirm-or-cancel), [this](https://stackoverflow.com/questions/29912933/determine-if-print-cancel-button-in-google-chromes-print-preview-is-clicked/32920693) and [this](https://stackoverflow.com/questions/30862903/capture-distinct-print-cancel-print-button-event-click) post all coming to the same conclusion. – Emiel Zuurbier Aug 31 '20 at 22:08
0

The onafterprint event occurs when a page has started printing, or if the print dialogue box has been closed. That's why the event fires with both buttons.

So, there's no way to know if the user clicked Save or Cancel.

LuisZuluaga
  • 145
  • 2
  • 7