Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q73 Is there a way to have a button on a page with frames print a specific frame?

You are here: irt.org | FAQ | JavaScript | Print | Q73 [ previous next ]

With the introdution of JavaScript 1.2, a print method can be used to invoke the print dialogue box:

window.print()

or:

parent.frameB.print()

works in Netscape Navigator 4+ and Internet Explorer 5 beta 2+.

Steve Nabors responds:

I discovered a way of making the above concept work in Internet Explorer 4.0 as well. Using the script provided in Q481 and a print button on FrameA.formA.printbutton, you can move focus to a visible form element (parent.FrameB.formB.textbox.focus()) in frame (Frame B)and submitting that form where the onsubmit="javascript:window.print()" for formB. Frame B will print. The key is moving focus to a visible form element on Frame B, otherwise it will print the Frame A where focus from the print button remains.

In your frame page, make FrameA and FrameB the frame names. HTML for framer page below:
<html>
<frameset rows="20%,*">
<frame SRC="framea.htm" name="FrameA" noresize>
<frame SRC="frameb.htm" name="FrameB" noresize>
<noframes>
<body>
</body>
</noframes>
</frameset>
</html>
HTML for framea.htm page below:
<html>
<head>
<script>
function printotherframe()
{
parent.FrameB.formB.textboxb.focus()
parent.FrameB.formB.submit()
}
</script>
</head>

<body>
<form name="formA"><input type="button" value="Print the other frame" onclick="printotherframe()">
</body>

</html>
HTML for frameb.htm page below:
<html><head>
<script language="JavaScript"></script>


<script language="VBScript">
sub window_onunload

on error resume next

' Just tidy up when we leave to be sure we aren't
' keeping instances of the web-browser control in memory
set WB = nothing
end sub

sub print

OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1


on error resume next

'Internet Explorer 4 object has different command structure
if DA then
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER)

else
call WB.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","")

end if

if err.number <> 0 then
if DA then ' Internet Explorer 4 they probably cancelled
alert "Nothing Printed :" & err.number & " : " & err.description
else
handle_error ' ie3x give alternate instructions
end if
end if
end sub

'This will be interpreted during loading.
'It will write out the correct webbrowser object depending
'on the browser version. To be sure it works, make sure you
'include this script block in between the body tags.

if DA then
'this must be Internet Explorer 4 or greater
wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
else
'this must be Internet Explorer 3.x
wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
end if

document.write "<OBJECT ID=""WB"" WIDTH=0 HEIGHT=0 CLASSID=""CLSID:"
document.write wbvers & """> </OBJECT>"
</script>

</head>
<body>
<form name="formB" action="javascript:window.print()">
<input type="text" name="textboxb" size="12" value="Print Me">
</form>
</body></html>

Ken Tholke writes:

I wrestled with this problem for nearly a week. Steve's solution reminded me that when you try to print from another frame, the frame you last clicked in is the frame with the focus. The following is what I came up with that works for Internet Explorer 4+, as well as Netscape Navigator 4, using a standard three frame set-up (as shown below).

<html>
<head>
<title>Parent Frame</title>
</head>
<frameset rows="90, *">
<frame name="header" src="name.html">
<frameset cols="18%, 82%">
<frame name="navigator" src="another_name.html">
<frame name="display" src="still_yet_another_name.html">
</frameset>
</frameset>
</html>

Again, the key is to put the focus on the frame that is supposed to be printed, by invoking "focus()" first. In still_yet_another_name.html:

<html>
<head>
<title>Navigator Frame</title>
</head>
<body>
<form name="PrintA">
<input type="button" name="PrintB" value=" Print " onClick = "parent.display.focus(); parent.display.print()">
</form>
</body>
</html> 

Feedback on 'Q73 Is there a way to have a button on a page with frames print a specific frame?'

©2018 Martin Webb