Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Print iframe contents only

This is my code

<script> var body = "dddddd"     var script = "<script>window.print();</scr'+'ipt>";  var newWin = $("#printf")[0].contentWindow.document;  newWin.open(); newWin.close();  $("body",newWin).append(body+script);  </script> <iframe id="printf"></iframe> 

This works but it prints the parent page, how do I get it to print just the iframe?

like image 747
user1083382 Avatar asked Mar 08 '12 10:03

user1083382


People also ask

How to print content of iframe JavaScript?

document. getElementById("print-iframe"). contentWindow. print();

How do you print something in JavaScript?

JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.


2 Answers

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

like image 104
mplungjan Avatar answered Oct 22 '22 06:10

mplungjan


document.getElementById("printf").contentWindow.print(); 

Same origin policy applies.

like image 24
Salman A Avatar answered Oct 22 '22 07:10

Salman A