Click here to Skip to main content
15,883,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Friends,
I found this JavaScript function on a Example of my friend.
Can someone plz explain me this function in details ?

JavaScript
function callPrint(elementId) {
                var prtContent = document.getElementById(elementId);
                var WinPrint = window.open('', '', 'left=0,top=0,width=1000,height=600,toolbar=2,scrollbars=2,status=0');
                var docColor = "Black";
                var strInnerHTML = prtContent.innerHTML;
                var strModifiedInnerHTMl = strInnerHTML.replace(/white/g, docColor);
                WinPrint.document.write(strModifiedInnerHTMl);
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                WinPrint.close();
            }   


I need Explanation from 3rd line Onward.

Thanks For Helping.
Posted

1 solution

JavaScript
var docColor = "Black"

Create a variable named docColor and set its value to the string "Black"
JavaScript
var strInnerHTML = prtContent.innerHTML;

Create a variable named strInnerHTML and set its value to the innerHTML value of the prtContent page element.
JavaScript
var strModifiedInnerHTMl = strInnerHTML.replace(/white/g, docColor);

Create a variable named strModifiedInnerHTML and set its value to a modified copy of strInnerHTML where any instances of "white" have been replaced by the current value of docColor(Which is currently "Black"). This is being done with a regular expression /white/ with a g switch indicating that it will update all instances that it finds.
JavaScript
WinPrint.document.write(strModifiedInnerHTMl);

Writes the strModifiedInnerHTML string to a WinPrint document object.
JavaScript
WinPrint.document.close();

Closes the WinPrint document
JavaScript
WinPrint.focus();

Gives the WinPrint object focus
JavaScript
WinPrint.print();

Sends the Winprint object to the printer
JavaScript
WinPrint.close();

Closes/Finished the WinPrint object. More than likely this call is required to clean up resources that the WinPrint object required to perform its actions.
 
Share this answer
 
v3
Comments
P_Dash 3-Oct-12 14:40pm    
Bro, just explain me abt "/white/g" in "strInnerHTML.replace(/white/g, docColor);".
This was my main doubt.
Also why "WinPrint.document.close();" is used ?

Plz Explain Me.
fjdiewornncalwe 3-Oct-12 15:35pm    
See Updated Answer.
P_Dash 3-Oct-12 16:00pm    
So Can We Mention Anything else in Place of 'g'
Means where can I find options use like this ?

Also plz Tell me why it used "WinPrint.document.close();"

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900