Click here to Skip to main content
12,512,130 members (54,176 online)
Click here to Skip to main content
Add your own
alternative version

Stats

43.5K views
522 downloads
22 bookmarked
Posted

Javascript Custom Parts Print Preview

, 30 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
a javascript module that grants developer to select variant parts or may be the whole page to make user can print preview it and print it.

Custom Parts Printer it’s a javascript module that grants developer to select variant parts or may be the whole page to make user can print preview it and print it.

Html elements accept the custom attributes. Any part of page needed to print out, it surrounded by html element Div with custom attribute “Printable”.

The print preview comes to user in popup window with print button if he/she likes to print it.

The main Script function was

DoPrintableSections(wdth,hght,stylesheets,imgsdir)

Where wdth and hght are the width and height of the popup window respectively.

Stylesheets is string which contains the style sheets files applied to the preview page separated by semicolons.

Imgsdir is the image directory which contains images need in print preview process like replacement of radio button with image.

Lets go in deep with main function and see how it’s working.

function DoPrintableSections(wdth,hght,stylesheets,imgsdir)

{

(*) CreateVirtualCarrier("ContentCarrier");

(*)var e = document.getElementsByTagName("div");

var content = "";

for(var i=0;i<e.length;i++){

   (*)  if(e[i].printable == "Print"){

        var cc = document.getElementById("ContentCarrier");

        cc.innerHTML = e[i].innerHTML;

   (*)  ClearInputs(cc,"INPUT",imgsdir);

        ClearInputs(cc,"select");

        ClearInputs(cc,"TEXTAREA");

   (*)  content = content + "<br/>" + cc.innerHTML ; 

    }   

}

(*)OpenPreviewPage(content,wdth,hght,stylesheets,imgsdir);

(*)cc.innerHTML="";

}

(*)First thing this function process is creating Virtual Carrier, It’s just a div we added to the html document to can use the DOM of browser in handling each custom HTML part respectively and to get the new handled html as string using magic method innerHTML.

function CreateVirtualCarrier(carriername){

    var vc = document.createElement('<div id="'+ carriername +'" style="width:600; height:200"></div>');

    document.body.appendChild(vc);

}

(*)Then we get all html div elements using getElementsByTagName() which provides the most performant way to iterate over all the elements in the DOM.

(*)After that make looping for the array of elements returned and check if it’s marked as printable or not. The Divs marked as printable we copy it’s html content to the virtual carrier and the start to handle it.

 

The handing process comes from Clears functions which replace or remove elements by checking there types. We use two main steps to do that

1-      Currentelement.parentNode.insertBefore 

2-     Currentelement.removeNode(true)

After all we put the collected generated HTML in new popup window that user will see it.





License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

emadmraghib
Team Leader
Saudi Arabia Saudi Arabia
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWill it work in all browser... Pin
mygapula sudheer8-Oct-10 19:01
membermygapula sudheer8-Oct-10 19:01 
GeneralEverything works except external CSS Pin
Joel Mathias26-Nov-09 2:31
memberJoel Mathias26-Nov-09 2:31 
GeneralMy vote of 1 Pin
rybrns7-Jun-09 5:34
memberrybrns7-Jun-09 5:34 
GeneralNot working Pin
rybrns7-Jun-09 5:27
memberrybrns7-Jun-09 5:27 
GeneralRe example Pin
special_me_198120-Oct-08 1:01
memberspecial_me_198120-Oct-08 1:01 
GeneralExcellent!!! Pin
Pentium.Disel6-Apr-08 10:40
memberPentium.Disel6-Apr-08 10:40 
QuestionExample? Pin
crb90003-Jan-08 5:26
membercrb90003-Jan-08 5:26 
AnswerRe: Example? Pin
Kanedogg0811-Jun-08 21:29
memberKanedogg0811-Jun-08 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 30 Dec 2007
Article Copyright 2007 by emadmraghib
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid