1

The script below works fine when there is a single button on a page. However, when I add a second button to a page using a second CEWP, calling up a different webpart ID than the one below (WebpartWPQ3), both buttons call up the same webpart (the button that is last on the page appears to be the one that is called up by both buttons). The only difference in the script for the second button is the WebPartElementID (WebpartWPQ4). Is there something else I need to change?

<input type="button" OnClick="javascript:void(PrintWebPart())" value="Print Risks/Issues">

<script language="JavaScript" type="text/javascript">
 //Controls which Web Part or zone to print
 var WebPartElementID = “WebPartWPQ3”;
 //Function to print Web Part
 function PrintWebPart()
 {
 var bolWebPartFound = false;
 if (document.getElementById != null)
 {
 //Create html to print in new window
 var PrintingHTML = ‘<HTML>\n<HEAD>\n’;
 //Take data from Head Tag
 if (document.getElementsByTagName != null)
 {
 var HeadData= document.getElementsByTagName(“HEAD”);
if (HeadData.length > 0)
 PrintingHTML += HeadData[0].innerHTML;
 }
 PrintingHTML += ‘\n</HEAD>\n<BODY>\n’;
var WebPartData = document.getElementById(WebPartElementID);
 if (WebPartData != null)
 {
 PrintingHTML += WebPartData.innerHTML;
 bolWebPartFound = true;
 }
 else
 {
 bolWebPartFound = false;
 alert (‘Cannot Find Web Part’);
 }
 }
PrintingHTML += ‘\n</BODY>\n</HTML>’;
 //Open new window to print
 if (bolWebPartFound)
 {
 var PrintingWindow = window.open(“”,”PrintWebPart”, “toolbar,width=900,height=700,scrollbars,resizable,menubar”);
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);
PrintingWindow.document.close();
 // Open Print Window
PrintingWindow.print();
 }
 }</script>

2 Answers 2

0

You have a parameterless function PrintWebPart() with the webpart id hardcoded within.

So only one of the JS function instant is called on the click of either Print button and printing the corresponding webpart.

Best you make a change in the function to accept a parameter webpartId and pass that from the button. This way you will have the functionality working.

Also note you will not require to add the JS function twice onto the page.

1
  • Thank you for the reply...could you show me what those edits would be. I would appreciate it. Aug 17, 2016 at 12:30
0
<input type="button" OnClick="javascript:void(PrintWebPart('WebPartWPQ3'))" value="Print Risks/Issues">

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

function PrintWebPart(WebPartElementID){
...
}

If you want to print a different WebPart, pass in the different WebPart ID into the PrintWebPart() function. Your next button may look like this:

<input type="button" OnClick="javascript:void(PrintWebPart('WebPartWPQ4'))" value="Print NEXT WEBPART">
2
  • Thank you Amanda....Does this get removed...var WebPartElementID = “WebPartWPQ3”; It may be my corporate site but it appears that when I use the original code...a new window opens....when I pass on the parameter as in your suggestion. No window opens. Aug 17, 2016 at 12:55
  • Yes, you're not longer setting the value of the variable equal to "WebPartWPQ3", you're passing it into the function instead. I would step through your code with your developer tools on the browser (F12), to see what is being passed in. The code to open the print page in a new window is inside an if statement. If the 'bolWebPartFound' is true, it will open the print window document, else it won't open the print window. Aug 17, 2016 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.