PDA

View Full Version : Help with Javascript and printing in a new window



SPSPilot
05-18-2006, 08:15 PM
Ok I have this page (main.html) that is loaded with an iframe (that loads in this mypage2.html) that I would like to print. How I would like it to print is when a user clicks on a Print link on the main.html page that it opens a new window with this mypage2.html and then prompts the print dialog box. After the user prints mypage2.html, the new window closes.

Well I have the following code that I added to the mypage2.html which is the page that gets loaded into the iframe:




<object id=WBControl width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></object>
<script language=VBScript><!-- //
Sub VBPrint() On Error Resume Next
WBControl.ExecWB 6,1
End Sub
//--></script>

<script language=JavaScript><!-- //
// if browser is not IE, FF or Netscape then just prints
if (window.print) self.print();
//Checks to see if browser is IE
else if (navigator.appName.indexOf('Microsoft') !=-1) VBPrint()
setTimeout('self.close()',3000);
else if (navigator.appName.indexOf('Netscape') !=-1) VBPrint()
setTimeout('self.close()',3000);
//--></script>


Then when a user clicks on the Print link back on main.html to open the mypage2.html, the code used on the link is:



<a href="javascript:;" onClick="window.open('mypage2.html','newwin')">Print this page</a>


Well this works out well in IE but in FireFox and Netscape everytime the main.html page opens with the iframe loading this mypage2.html, Firefox and Netscape wants to print the parent page .

So my question is there is someway to make Firefox and Netscape do a similar function? Or am I just way off on my code?

Thanks for the advice.

djr33
05-18-2006, 11:12 PM
this isn't definite, but I would guss that FF and NS don't support your code.
closing a window once printed is kinda vague and might not be a command supported by all browsers.
Good luck though.


So my question is there is someway to make Firefox and Netscape do a similar function?Oh... just saw that.
Right... I think you may very well need a new code for it, if there is one... I'm not sure.


Also, "works" means what?
Does:
1. the window open?
2. the printing dialogue box pop up?
3. it print?
4. the window close?

'cause... if it's just the window not closing, then who cares... FF/NS users can just close the window. If it's more than that, not sure... especially about the auto print function... but.. again, why not just let them choose print from the menu... you could setup an alert (something like-- if browser=FF/NS, alert("click print, then close window").) If you want, that is.

Twey
05-18-2006, 11:33 PM
Please, please, please read the post?
Well this works out well in IE but in FireFox and Netscape everytime the main.html page opens with the iframe loading this mypage2.html, Firefox and Netscape wants to print the parent page .Try this.
<object id="WBControl" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
<script language="VBScript">
<!--
Sub VBPrint() On Error Resume Next
WBControl.ExecWB 6,1
End Sub
' --></script>

<script type="text/javascript">
var printfunc = (window.print || VBPrint || false);
if(printfunc && this.name == "printwin") {
printfunc();
setTimeout('self.close()',3000);
}

function printThis() {
window.open(this.location.href, "printwin");
}
</script>

djr33
05-19-2006, 01:03 AM
Ah, ok. I missed a couple things... sorry... distracted.

SPSPilot
05-19-2006, 02:57 PM
Thanks Twey. The solution you provided worked out well and I can see where I went wrong with my code the first time.