jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How do I get a print preview window to display from JavaScript?
Derived from A question posed by John Zukowski PREMIUM
Topics JavaScript:Syntax
Author Ravi Verelly
Created Sep 5, 2001


Answer
Hope this helps:
<html>
<head>
<title>Print Preview</title>
<script>
function printpr()
{
var OLECMDID = 7;
/* OLECMDID values:
* 6 - print
* 7 - print preview
* 1 - open window
* 4 - Save As
*/
var PROMPT = 1; // 2 DONTPROMPTUSER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";
}
</script>
</head>
<body>
<form>
<input type='button' value="Print Preview" onclick="printpr();">
</form>
</body>
</html>


Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Comments and alternative answers

Comment on this FAQ entry

Re: Print Preview in JavaScript
Venugopal V.V., Sep 5, 2001  [replies:1]
The above code works only in IE?

Thanks,

Venu

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re[2]: Print Preview in JavaScript
Sudhakara TP, Sep 7, 2004
Hi, Its works perfectly. But now I have another problem in that. I have an hyperlink in my page onclick of that will close the window and in the same page, I have a print button on click of which i need to display the print preview for which i have used the code. Now, when i generate the print preview i need to hide these hyperlinks on the page and to do this task, i am temporarily hiding these hyperlinks, generating the print preview & then showing these hyperlinks. Till here everything is working perfectly. Now, when I generate the print preview, and when I click on the close button in the print preview window, click on the goback hyperlink, the window is being closed. In the other hand, when i generate the print preview, close the print preview window by clicking on the closable button on the title bar, click on the goback button, the window is not getting closed. Not able to figure out the problem. Could any one help me on this. My email id is spotsudhi@yahoo.com Expecting an early reply from you Regards Sudhakara.T.P.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
MSDN Article
John Zukowski PREMIUM, Sep 7, 2001
Scripting Support for Web Page Printing is from March 2000 and seems to infer that it works with IE4.0+.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Print preview with framed pages
Igor Portnoy, Aug 20, 2002
When I try to use this code on a page that is a part of frameset, the print preview shows the whole frameset, and not just the page on which this script is located. Is there way to modify this script to just preview the page on which it is located and not the whole frameset?

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  
Getting error
Priyadarshi Soumendra, Apr 13, 2004
Hi, I have used the code.But I am getting insertAdjacentHTML method is not a function. I am very new to javascript.So kindly suggest me what to do next? Regards, Soumendra

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

JavaScript FAQ

JavaScript Forum

Netscape JavaScript Reference

Focus on JavaScript

Wish List
Features
About jGuru
Contact Us

 


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers