Click here to Skip to main content
Licence CPOL
First Posted 30 Dec 2007
Views 33,087
Downloads 281
Bookmarked 22 times

Javascript Custom Parts Print Preview

By | 30 Dec 2007 | Article
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)

About the Author

emadmraghib

Team Leader

Saudi Arabia Saudi Arabia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionWill it work in all browser... Pinmembermygapula sudheer19:01 8 Oct '10  
GeneralEverything works except external CSS PinmemberJoel Mathias2:31 26 Nov '09  
GeneralMy vote of 1 Pinmemberrybrns5:34 7 Jun '09  
GeneralNot working Pinmemberrybrns5:27 7 Jun '09  
GeneralRe example Pinmemberspecial_me_19811:01 20 Oct '08  
GeneralExcellent!!! PinmemberPentium.Disel10:40 6 Apr '08  
QuestionExample? Pinmembercrb90005:26 3 Jan '08  
AnswerRe: Example? PinmemberKanedogg0821:29 11 Jun '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 Dec 2007
Article Copyright 2007 by emadmraghib
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid