Avatar of NEILPH
NEILPHFlag for New Zealand

asked on 

How can I click IE6+ toolbar Print button to avoid opening print dialog?

I want to put a button on my web page, that the user will click to print the page.

I know that I could put javascript behind the button to force the Ctrl-P keystroke, thus initiating the printing process. Unfortunately that brings up the Print dialog box.

I've noticed alos that if I manually click the Print button on my standard IE toolbar it prints without bring up the Print dialog box.

I know it is generally forbidden to put code on a page that invokes printing without user intervention, but I'm not trying to do that. I simply want javascript to click\keystroke the Print toolbar button.

JavaScript

Avatar of undefined
Last Comment
Saqib Khan
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

no need to invoce Ctrl + P. javaScript has a buil-in Method window.print()

Click to Print This Page

And also you cant avoid Windows Print Dialouge Box with javaScript.
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Yes, I know about window.print(), but like Ctrl+P it opens the print dialog.

Are you saying that it's impossible to click ANY toolbar button programmatically, using javascript or something else?

Surely it can be done. It is possible to focus on just about everything on a browser. The simply invoke a Click
Avatar of Loganathan Natarajan
according to me, it depends on the BROWSER settings ... we can do some extend in IE to avoid the dialog box while window.print();
SOLUTION
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Thanks everyone. I always understood those points about print().

Components looks interesting, but I did a quick visit to Microsoft's component gallery and got the feeling that this is old technology, [several of their page links wouldn't work, and there were several references to IE4, etc].

Loqudotcom, I'm not sure which browser settings you are referring to. It will be impractical to ask every user to change their browser settings.

These seem to be the facts as I see them. I'll see if anyone has anything to add. Then I'll award points.

1. Users of IE6+ can avoid the print dialog by clicking the toolbar Print button
2. There is no keystroke equivalent to clicking the toolbar Print button??????
3. Javascript can't invoke keystrokes????????????
4. There is no way to invoke a keystroke from within a web page?????????
5. Developers for public kiosks tell me that it is possible to avoid the print dialog.

>> Loqudotcom, I'm not sure which browser settings you are referring to. It will be impractical to ask every user to change their browser settings.

I meant, there is way to do it in only for IE browser settings on the printer options.. of course it also depends on the OS...
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Loqudotcom, are you saying that I could set the users printer options by use of javascript or some such non-manual method? If so, could you show me some code that would do it?
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Loqudotcom, I mean't to add that it would need to be do-able in IE6+ browsers
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Everyone may be interested in http://p2p.wrox.com/topic.asp?TOPIC_ID=23511 where they show how to avoid the print dialog in IE browsers by using a mixture of Javascript and VBScript.

There's a lot of meat in the discussion at that forum plus an interesting link to http://imar.spaanjaars.com/QuickDocID.aspx?QUICKDOC=243 and an interesting Google search http://www.google.com/search?q=print+without+dialog. I'm studying these. My design brief is for IE6+ browsers so there might be something there.

Comments anyone? Or is this really a dead duck?
>> are you saying that I could set the users printer options by use of javascript or some such non-manual method? If so, could you show me some code that would do it?

sorry, i never tried that ., but i felt that it can be done in any way though there is security issue  on it.
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

the solution in that /codingforums/ link is very interesting.

Sure, it's about programmatically setting print properties, which is not what I want to do, but it does have 2 interesting features....

1. it uses javascript to get right into the guts of playing with print functioning

2. it looks like the javascript is using shell.sendKeys("%a{TAB}.2{TAB}0{TAB}0{TAB}0{ENTER}"); to simulate a person walking their way around the Print Dialog box and making selections. This gives me hope that Javascript could simulate focusing on the browser toolbar and clicking the Print button if it finds it.

So where do we go from here? I'll study the /codingforums/ stuff further to see if I can pinpoint functions that could hold potential [e.g. shell.sendKeys()], but coding it would be way beyond me. How about you?

******************************
An interesting excerpt from  http://codingforums.com/archive/index.php?t-36037.html

<script language="javascript" type="text/javascript">

var shell = new ActiveXObject("WScript.Shell");
function SetPrintProperties() {
try {
window.setTimeout("javascript:shell.SendKeys('%fu');",1000);
window.setTimeout("javascript:SetPaperSize();", 1000);
} catch (e) {
alert ("An exception occured: " + e + "\nCode is: " + e.number + "\nDescription is: " + e.description);
alert('Please verify that your print settings have a Landscape orientation and minimum margins.');
}
}

function SetPaperSize() {
shell.sendKeys("%a{TAB}.2{TAB}0{TAB}0{TAB}0{ENTER}");
}
</script>

Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Whoops. I got diverted from exploring Loqudotcom's sendKeys possibility as per previous comment, will get back onto it.

In the meantime, on a forum I found the code in the snippet. Experts will not be surprised to know that it successfully prints without a dialog in IE6, when run on my desktop. But when I upload it to the web and then run it I get error dialog "An ActiveX control on this page is not safe. Your current security settings prohibit running unsafe controls on this page. As a result this page may not display as intended."

In other words it won't let users create the required ActiveX without playing with their security settings. Ah well!

<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>nodialog.htm</title>
 
 
<script language="javascript">
function print_explorer() 
{ 
var OLECMDID_PRINT = 6;
var OLECMDEXECOPT_DONTPROMPTUSER = 2;
var OLECMDEXECOPT_PROMPTUSER = 1;
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
oldHandler = window.onerror; 
WebBrowser1.ExecWB(OLECMDID_PRINT, -1);
WebBrowser1.outerHTML = ""; 
window.onerror = oldHandler;
}
</script>
  </head>
  <body>
<p>Hello</p>
<form action="">
<input type="button" value="Print this page without dialog" onclick="print_explorer()">
</form>
 
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of NEILPH
NEILPH
Flag of New Zealand image

ASKER

Thanks guys. I've allocated the points according to how many times each person replied:
adilkhan 2 replies = 125 points
Loqudotcom 6 replies = 375 points

"How to print without the dialog" has to be one of the most frequently asked questions on forums.

The answer is, yes it can be done but only in a restricted environment because of the security risk. I proved this. The example in my earlier snippet works when run on my PC but not when run from the Web. Using Sendkeys to mimic manually opening and closing print dialog would probably work too. But both create ActiveX components that won't run on the open internet without changing browser security settings.

I will explore creating a customised toolbar with a large print button. This is the only other "easy" option I can think of. For those happy with a controlled environment, the product SiteKiosk at www.proviso.com looks impressive.
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

I will stick to my orignal post. you cant just do it.

it is not possible to satisfy your internet audience and do what you want with the print command.

imagine you automate print button on a porn website to print of images or "do Spam Via Printer" (Thank God, pop ups are already too much to handle).
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo