Can you make an event after a Javascript impression?

0

I would like to do a post impression action, ie only when the user confirms the impression that the javascript will execute a scheduled event. If I put an event under window.print() javascript executes even without the user having printed yet. Something like:

window.print() = function(){
    [...]
}

I heard about matchMedia , but I did not understand. Would you like to exemplify from the code below?

1) Before printing I want the background of the yellow screen;

2) After printing I want the background of the green screen;

<html>
	<head>
		<title>teste</title>
		<script>
			function imprimir() {
				window.print()
			}
		</script>
	</head>
	<body>
		<input type="button" value="imprimir" onclick="imprimir()" />
	</body>
</html>
    
asked by anonymous 12.05.2017 / 16:31

1 answer

0

I found this snippet of English version that seems to solve the question:

(function() {

    var beforePrint = function() {
        console.log('Antes de imprimir...');
    };

    var afterPrint = function() {
        console.log('Depois de imprimir...');
    };

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

    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;

}());

More details about MatchMedia in the original question here >.

    
12.05.2017 / 17:31