Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Print Preview be called from Javascript?

I have a page that is supposed to launch the Print Preview page onload.

I found this:

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 = ""; 

But...

  1. it does not work in FireFox.
  2. it's kind of ugly.

Is there a better way for IE or a way that works for FireFox?

like image 244
Rich Bennema Avatar asked Oct 23 '08 15:10

Rich Bennema


People also ask

How do you print screen in JavaScript?

Window print() Method The print() method prints the contents of the current window. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.

Is there a print function in JavaScript?

JavaScript PrintJavaScript 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.

What is called print preview?

Print Preview is a functionality that lets users see the pages that are about to print, allowing the users to see exactly how the pages will look when they are printed.

Which command is used for print preview?

Press Ctrl + F2 to open the print preview for the document you're currently viewing.


1 Answers

You can't, Print Preview is a feature of a browser, and therefore should be protected from being called by JavaScript as it would be a security risk.

That's why your example uses Active X, which bypasses the JavaScript security issues.

So instead use the print stylesheet that you already should have and show it for media=screen,print instead of media=print.

Read Alist Apart: Going to Print for a good article on the subject of print stylesheets.

like image 196
svandragt Avatar answered Sep 20 '22 20:09

svandragt