Hi and thank you for your response.
I’m facing a problem when attempting to open a new tab in Chrome and Edge and then print the content of that tab. My project is built using Angular.
Steps to Reproduce:
Step 1: From Tab 1, click a button to open a new tab. After the new tab is loaded, it automatically calls window.print();
.
print() {
// Your content
let content = `
<html>
<head>
<title>Print Page</title>
</head>
<body onload="window.print()">
Content
</body>
</html>
`;
// Create a Blob from the HTML content
let blob = new Blob([content], { type: 'text/html' });
// Create a URL for the Blob
let url = URL.createObjectURL(blob);
// Open a new tab with the created URL
window.open(url, '_blank');
// Release the URL object after the new tab is opened
URL.revokeObjectURL(url);
}
Step 2: Close the newly opened tab (click ‘x’ icon).
After following the outlined steps, the new tab opens successfully, but upon closing the tab using the ‘x’ icon, Tab 1 (the original tab) becomes partially or fully disabled. This results in a loss of interactivity, and the issue persists until a manual refresh of Tab 1.
Expected Behavior:
Closing the newly opened tab should not affect the functionality or disable any elements in Tab 1.
Code Sample:
Demo Link:
Thanks again for your valuable help!
2
Answers
One way to address this issue is to separate the printing functionality from the tab opening process. Instead of automatically invoking
window.print()
upon opening the new tab, you can let the user trigger the printing action manually. Here’s how you can modify your code to achieve this:Answer from Sasi Kumar M ( please upvote this answer also! ) Finally helped me solve your issue, please call the
window.open
with the third parameternoreferrer
which does not pass a reference to the opening tab and this seems to fix the issue!!Please note stackblitz may not work, I found the solution in my local system, would suggest you to do the same!
Stackblitz Demo