Andrew
Andrew

Reputation: 7458

Capture (distinct) Print / Cancel Print Button Event (click)

How can I capture only the cancel event from the print page?

I have a page that on a button click you have a new tab opened with print loaded, there if you click cancel (not print) I have to call a method. Same for print.

In case of print a want to execute PrintSuccesfully(), if Cancel print I want to execute PrintCancelled().

My code that opens the new tab:

var windowObject = window.open('', '_blank');
windowObject.document.write(generatePrintHTMLBody(scope));
windowObject.document.close();
windowObject.focus();
windowObject.print();
windowObject.close();

I looked up onbeforeprint and onafterprint, but both are executed with matchMedia, on CTRL+P event or on my code at Print / Cancel button click.

The code I tested was included in the generatePrintHTMLBody(scope) found here:

(function() {
    var beforePrint = function() {
        console.log('Functionality to run before printing.');
    };
    var afterPrint = function() {
        console.log('Functionality to run after printing');
    };

    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;
}());

Upvotes: 3

Views: 3799

Answers (1)

Andrew
Andrew

Reputation: 7458

After a lot of research it seems you cannot do this... The behaviour si diferent for the buttons on each browser or it has the same signature, so no resolution exists.

Upvotes: 3

Related Questions