How to manually close print preview of a browser using JavaScript or Typescript

  angular, javascript, jquery, typescript

I have been trying many solutions but nothing helps me to solve my problem.

I have a scenario when a user trying to print a webpage from the browser menu Print option I have to redirect them to a different tab with the print version of the parent page, which I’m able to do it using the below listener.

@HostListener('window:beforeprint', ['$event'])
onBeforePrint(event) {
window.open(printUrl, '_blank');
setTimeout(() => {
const escapeEvent = new KeyboardEvent("keydown", {
     'key': 'Escape'
  });
  document.dispatchEvent(event);
})
}

Now until navigation is fine but on my parent screen the print preview is still open and its show the default page view.

I have tried checking all possible ways of trying to close or capture the cancel button event from the print preview of the browser but no luck.

Any help will be highly appreciated.

Source: Ask Javascript Questions

LEAVE A COMMENT