JavaScript Print Iframe Contents Only

Javascript Print iframe contents only

I would not expect that to work

try instead

window.frames["printf"].focus();
window.frames["printf"].print();

and use

<iframe id="printf" name="printf"></iframe>

Alternatively try good old

var newWin = window.frames["printf"];
newWin.document.write('<body onload="window.print()">dddd</body>');
newWin.document.close();

if jQuery cannot hack it

Live Demo

Print iFrame content using Javascript

.contentWindow.print(); worked for me.

I've opened the PDF in iframe of fancybox and then called .contentWindow.print();.

Though it's not supported in some older browsers.

trying to print iframe content but print the whole page instead

You may first check if the user agent is IE:

function printIFrameContent()
{
var ua = window.navigator.userAgent;
var msie = ua.indexOf ("MSIE ");
var iframe = document.getElementById("testFrame");

if (msie > 0) {
iframe.contentWindow.document.execCommand('print', false, null);
} else {
iframe.contentWindow.print();
}
}

Referred a similar answer on SO: https://stackoverflow.com/a/19639241/1500851



Related Topics



Leave a reply



Submit