Just Talk About Web

Techniques Behind Modern Web

Ajax - JavaScript




Searching a solution for how to bypass printer dialog box when printing multiple web pages in Internet Explorer which is required in one of my company’s projects I found a hack from DevX.com.

1
2
3
4
5
6
7
8
9
10
if (navigator.appName == "Microsoft Internet Explorer")
{
     var PrintCommand = '<object ID="PrintCommandObject" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
     document.body.insertAdjacentHTML('beforeEnd', PrintCommand);
     PrintCommandObject.ExecWB(6, -1);
     PrintCommandObject.outerHTML = "";
}
else {
    window.print();
}

But it does not work in Windows XP SP2 (and Windows Server 2003 SP1 or above). Further researches reveal that the augments for printing command should be:

PrintCommandObject.ExecWB(<strong>6, 2</strong>);

Another problem is IE considers the print ActiveX control not “safe” (though it’s a native one, no?) so you must enable “Initialize and script ActiveX control not marked as safe” option under site security settings (see picture below).

ie6-security-settings.gif

Please note that lowering security settings in IE should be made with care. You should add the app address to “Trusted sites” zone and set security settings as above for that zone only.

Finally, I come to the scripts below that support fallback to normal JavaScript print command “window.print()” when the ActiveX is unable to initialize or printing from other browsers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var PrintCommandObject = null;
 
function printPage(){
 if(PrintCommandObject)
 {
 	try{
 		PrintCommandObject.ExecWB(6, 2);
 		PrintCommandObject.outerHTML = "";
 	}
 	catch(e){
 		window.print();
 	}
 }
 else{
 	window.print();
 }
}
 
window.onload = function(){
 if (navigator.appName == "Microsoft Internet Explorer"){
 	// attach and initialize print command ActiveX object
 	try{
 		var PrintCommand = '<object id="PrintCommandObject" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" width="0" height="0"></object>';
 		document.body.insertAdjacentHTML('beforeEnd', PrintCommand);
 	}
 	catch(e){}
 }
 
  setTimeout(printPage, 1);
};

In Firefox, the solution is much simpler: just type “about:config” to address bar and add a new Boolean preference item named “print.always_print_silent” then set value of the newly-added item to “true“. Done!

firefox-about-configs.gif

(Note: may you want to disable printing progress dialog, just set “print.show_print_progress” item value to “false” too.)

Hope this would help those who are struggling with the same issue. Please share your solutions for Safari, Opera…? Thanks.

Share and Enjoy:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • DZone
  • del.icio.us
  • Reddit
  • Mixx
  • Technorati
  • Sphinn
  • Facebook
  • Google
  • SphereIt
  • BlinkList
  • Furl
  • Ma.gnolia
  • Slashdot
  • Spurl
  • TailRank
  • TwitThis

As you may notice, I've used Ext JS in a couple of my own projects. I can say Ext JS is a professional project and what Jack Slocum has been doing are better than any other projects in term of UI components: with a few lines of JavaScript codes, you can create an excellent UI that gives you a new ...

  • 5 Comments
  • Filed under: Ext JS
  • After installing Firefox 3 Beta 5, I found Firebug did not work properly and that led me to the 1.2 branch of this great add-on, in alpha at this time. This build can chuck out some issues but not all. However, the first beta of this branch is expected to come out soon and it should work well with the ...

    Ext JavaScript Plugin for Grails

    Marcel Overdijk has released Ext plugin for Grails. This plugin provides integration with the Ext JavaScript Library. When installing the plugin, it downloads and installs automatically the latest Ext 2.0.2 distribution in your application, and registers itself to be used with the adaptive AJAX tags. It also contains two helper tags to easily include additional Ext JavaScript and CSS files as ...

  • 0 Comments
  • Filed under: Ext JS
  • Firefox 3.0 Beta 5 is now available for download. This release includes "750 changes from the previous beta, improving stability and web compatibility, providing platform and user interface enhancements, and resulting in the fastest Firefox ever. Many of these improvements were based on community feedback from the previous beta" (quoted from the release note). Adrian had a little hands-on time with ...

    A few weeks ago, I posted a comparison of Dojo ShrinkSafe, YUI Compressor and Jasob Obfuscator. Then, I received comment from Brian introducing an interesting project: Include; some day later it was covered by Ajaxian with a few good comments that are worth a read. So, I decided to integrate into one of my projects to have hand-on experience with ...

    JavaScript Number Format Function

    This is another useful JavaScript function by Matt Snider used for number formatting. For JavaScript being considered "the world's most misunderstood programming language", Matt tried to explain how number is treated in JS (read more here). Here are build-in functions of Number object supported by different IE and Firefox versions. method IE FireFox Explaination toExponential 5.5 1.5 Returns the expotential value of the number. toFixed 5.5 1.5 Returns a number with a ...

    For a heavy JavaScript web application, one of the most important things you should do before making it public is to obfuscate and compress JavaScript and CSS. The reason to do that is obvious: your app will be faster and safer. When the web code is not optimized, it makes your visitors waste bandwidth and wait longer for the page to ...

    Firebug for Firefox 3

    If Firebug is only reason to prevent you from upgrading to Firefox 3 (beta) you can do that right now because the latest version (1.1, beta too) of Firebug works nicely with it. Among a lot of bug fixes, some great features of this version are: Supports Firefox 3, eval() debugging, browser-generated event handler debugging, executable lines marked with ...

    Benefits of GWT in 61 Slides

    Didier Girard releases 61 slides introducing why GWT is better solution for developing modern web apps. | View | Upload your own Benefits to be highlighted are: GWT is simple: Developing a web application with GWT is as simple as developing a Windows application with VB and it supports MDI apps too GWT cuts costs: Building a GWT application is 5 times faster ...

  • 2 Comments
  • Filed under: GWT








  • Free Advertising Free Annual Credit Report

    BlogRush


    Jobs

    Web Hosting

    DreamHost promotion code (use it on registration):

    JTAW - $50 off for all plans ($30 off for monthly plan).

    JTAW2 - 1 extra FREE lifetime unique IP and $30 off for one year or above plans.

    Advertisements

    Blog Promotion