Hi all,

I want to print a page from another page. I have a master page with links to other page. When I click "print," it should print the another page.
The function window.print() is printing only the active page. So on click print i am opening a new window.

In the onload event of the target page i have used
<body onload="window.focus();window.print();window.close ();">

Eventhough it's working, at the same time the window is also popping up behind the printer set up screen. when i click print or cancel the window gets closed automatically.
i don't want the window to pop up. Do suggest me if there is any other alternative way..

Thanks for your help.

Dani AI

Generated

A few practical ways to avoid a visible popup when printing “another” page. As discovered, opening a new window and calling print() tends to flash a window behind the print dialog. ’s in-page approach is the right idea — avoid a separate browser window. Two reliable patterns that work in most cases are (A) fetch the target HTML into the current page and print a dedicated container, or (B) create a temporary off‑screen iframe, trigger its print, then remove it.

Option A — fetch + print container (same-origin or proxied)

async function printInline(url){
  const res = await fetch(url, { credentials: 'same-origin' });
  let html = await res.text();
  // remove scripts for safety (or sanitize properly)
  html = html.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, '');
  const box = document.createElement('div');
  box.id = 'printArea';
  box.innerHTML = html;
  document.body.appendChild(box);
  document.documentElement.classList.add('printing');
  window.print();
  document.documentElement.classList.remove('printing');
  box.remove();
}

Required CSS (hide everything except the print area):

@media print {
  body * { display: none !important; }
  .printing #printArea { display: block !important; }
}

Option B — dynamic off‑screen iframe (good when you can load the page same-origin)

function printUrl(url){
  const ifr = document.createElement('iframe');
  ifr.style.position = 'absolute';
  ifr.style.left = '-9999px';
  ifr.src = url;
  document.body.appendChild(ifr);
  ifr.onload = () => {
    try { ifr.contentWindow.focus(); ifr.contentWindow.print(); }
    catch(e) { console.error('Cannot access frame (cross-origin).', e); }
    setTimeout(() => ifr.remove(), 1200);
  };
}

Notes and troubleshooting

  • Neither method can suppress the browser print dialog on normal user installs; silent printing requires kiosk mode / enterprise policies or a native helper.
  • Same-origin rules matter: if the page is cross-origin you can’t call contentWindow.print(); use a server-side proxy or produce a PDF to serve instead.
  • Don’t use display:none on the printable node — hide it off-screen (left:-9999px) so it renders for print.
    Test across your target browsers and fix relative URLs (base href or rewrite) when you pull raw HTML. These approaches avoid opening a visible popup while keeping the experience predictable.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

I think the page has to be open before you print it - but you can use an invisible iframe (cheat):

<script type="text/javascript">
function printPage(iFid){
iFid.focus();
iFid.print();
}
</script>
<style>
#iframe1{
visibility:hidden;
}
</style>
</head>
<body>
<iframe name="iframe1" id="iframe1" width="1" height="1" src="index.php"></iframe>
<a value="print page" href="javascript:printPage(iframe1);">print page</a>
</body>
</html>

I got this idea from here and just tinkered with it.

That's good idea. It worked for me. Thanku for your time and for detailed response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.