1

I have an app with possibility to print some contents. For that purpose I'm opening a new window and inserting there some layout.

// function body

const printWin = window.open('');

printWin.document.body.appendChild(el);

printWin.focus();
printWin.print();
printWin.close();

The problem is that unit print window is close the app is totally blocked (since it's executed synchronously).

I've tried to make

printWin.addEventListener('DOMContentLoaded', () => {
  printWin.focus();
  printWin.print();
  printWin.close();
})

However, this event doesn't seem to be fired. How do I print and immediately close window while not causing execution block?

2
  • You want to open the print dialog and close the window behind it? That's probably not gonna happen. Commented Sep 19, 2019 at 12:29
  • Instead of this, try to give the contents to an iframe, and then call that iframe's .print() method. Commented Sep 19, 2019 at 12:31

1 Answer 1

1

I've just tried what you are saying (Firefox 69.0- Ubuntu) and javascript (event loop) is not being blocked:

I used a webapp I'm working on and I could push data to it (that got displayed) through websocket.

What is blocked is browser's user input, because Firefox is forcing you to attend the print dialog box (either to confirm or cancel the task).

But the javascript is working as usual. Just you won't receive any mouse or keyboard events because you are temporally disconnected from them.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't want to be "disconnected". I need to be able to keep working even if there is opened print in other window
I understand but you can't because this is not about DOM or Javascript: It is about the browser itself who focuses IO only towards the print modal. Your best bet (if you can decide) is trying other browsers…
Also (but just in case of corporate web apps) you could print from the server (to network or shared printer(s)). But it will be a lot more tricky…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.