Click to See Complete Forum and Search --> : Problems printing an iFrame


elemental
05-06-2004, 12:03 PM
Firstly I'll own up to not being a code monkey, so I really do need some help.

I require a javascript code to focus and print an iFrame called 'stocklist_content' I need to be able to do this by placing a link in the parent frame and a seperate within the iFrame itself.

I have found a few articles on the subject but no complete examples of the required code.

Can anyone provide a full example?

Thanks in advance, elemental

Kor
05-06-2004, 12:11 PM
focus() the iframe than print() it (by the way, print() method works on IE only).

sciguyryan
05-06-2004, 12:14 PM
Try something like this:


<script type="text/javascript">
<!--
function PrintF(){
var IFRAME = document.getElementById("MyIframe");
IFRAME.print();
}
//-->
</script>


Note: remember to change the ID ("MyIframe") there to your frames id and, to add the event to something e.g. a button like so:


<form action="" method="post">
<input type="button" value="Print!" OnClick="PrintF();" />
</frame>

elemental
05-06-2004, 12:39 PM
I'm very impressed with the prompt replies, thanks.

sciguyryan I have used your code e.g.

http://www.sentinalcars.com/stocklist.html

but get the following error in Explorer 6:

IFRAME.print();Error: Object doesnt support this property or method on line 45

Line 45 being:

IFRAME.print();

err....

elemental
05-06-2004, 12:42 PM
Dam sorry typo in URL

http://www.sentinelcars.com/stocklist.html

elemental

sciguyryan
05-06-2004, 01:47 PM
Ok, sorry about that it seems I used an incorrect idea :)

try this:


<script type="text/javascript">
<!--
function PrintF(){
document.FrameName.focus();
document.FrameName.print();
}
//-->
</script>


Remember to change the names though :)

elemental
05-06-2004, 02:25 PM
No change

Error: Object doesn't support this property or method

Refers to Line 44:

[QUOTE]document.stocklist_content.print();[\QUOTE]

Next... ;)

sciguyryan
05-06-2004, 02:41 PM
Thats odd, when I looked this up that was the syntax used on another site :S

If I get any other ideas I'll post them for you to try.

fredmv
05-06-2004, 02:45 PM
http://www.webdeveloper.com/forum/showthread.php?s=&threadid=24900#post130635

elemental
05-06-2004, 03:08 PM
We're back on track, thanks fredmv.

Now the iframe content prints but not fully... it cuts off at the bottom of the printed page border...

Any ideas?

elemental
05-07-2004, 04:38 PM
The responses have stopped flowing... ok let me rephraise the question before I abandon iframes forever. How do I force the browser to print the entire contents of the iframe?

thanks in advance, elemental.

Vladdy
05-07-2004, 04:49 PM
Abandoning iframes is the best idea of the thread so far (most probably you were using them for all the wrong reasons anyway). :thumbsup:

ecline
02-20-2008, 05:11 PM
Tested in IE7

<head>
<script type="text/javascript">
function ClickHereToPrint(){
try{
var oIframe = document.getElementById('ifrmPrint');
var oContent = document.getElementById('divToPrint').innerHTML;
var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
if (oDoc.document) oDoc = oDoc.document;
oDoc.write("<html><head><title>title</title>");
oDoc.write("</head><body onload='this.focus(); this.print();'>");
oDoc.write(oContent + "</body></html>");
oDoc.close();
}
catch(e){
self.print();
}
}
</script>
</head>


<body>

<a onclick="ClickHereToPrint();">Print</a>

<iframe id='ifrmPrint' src='#' style="width:0px; height:0px;"></iframe>
<div id="divToPrint">
content
</div>
</body>