Determine If Print/Cancel Button in Google Chrome's Print Preview is Clicked Determine If Print/Cancel Button in Google Chrome's Print Preview is Clicked google-chrome google-chrome

Determine If Print/Cancel Button in Google Chrome's Print Preview is Clicked


You can not access Chrome's internal windows (printing dialog in this case) directly from a regular web page.

    (function () {        var beforePrint = function () {            alert('Functionality to run before printing.');        };        var afterPrint = function () {            alert('Functionality to run after printing');        };        if (window.matchMedia) {            var mediaQueryList = window.matchMedia('print');            mediaQueryList.addListener(function (mql) {                //alert($(mediaQueryList).html());                if (mql.matches) {                    beforePrint();                } else {                    afterPrint();                }            });        }        window.onbeforeprint = beforePrint;        window.onafterprint = afterPrint;    }());

Or, If you want to do something when the print preview gets opened, you can try below:

$(document).bind("keyup keydown", function (e) {         if (e.ctrlKey && e.keyCode == 80) {             setTimeout(function () { CallAfterWindowLoad();}, 5000);             return true;         }     });    function CallAfterWindowLoad()    {        alert("Open and call");    }

Reference: How to capture the click event on the default print menu called by Javascript window.print()

Maybe if you provide your requirements for this two buttons click event, we can provide you an alternate solution.


it is very easily possible:

<body onafterprint="myFunction()">

The myFunction() that you can define within a tag will be fire when either the printing job is done or the cancel button was pressed.