Can you make an event after a Javascript print?

Asked

Viewed 290 times

-1

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

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

I heard about matchMedia, but I didn’t understand. It would have as an example from the code below?

1) Before printing want yellow screen background;

2) After printing want green screen background;

<html>
	<head>
		<title>teste</title>
		<script>
			function imprimir() {
				window.print()
			}
		</script>
	</head>
	<body>
		<input type="button" value="imprimir" onclick="imprimir()" />
	</body>
</html>

1 answer

0

I found this piece of code on english version which seems to resolve the issue:

(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 author of the original question here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.