Stump the Panel » End Users and Information Workers

Code Review

(8 posts)
  1. malspa
    Member

    I am stumped. I have this code in a CEWP on a site. After three days of working on this, I need help. I know this is sloppy, but can someone tell me if they see anything wrong with the script

    <TABLE CELLPADDING="2" CELLSPACING="2" WIDTH="100%">
    <TR>
    <TD><div onkeydown="javascript:if (event.keyCode == 13) _SFSUBMIT_"><input type="text" name="T1"/><input type="button" value="Search" onclick="javascript:_SFSUBMIT_"/></div></TD>
    <TD><input type="button" OnClick="javascript:void(PrintWebPart())" value="Print"></TD>
    </TR>
    </TABLE>
    
    <script language="JavaScript">
    var WebTitleElementID = "WebPartWPQ3";
    var WebPartElementID = "WebPartWPQ5";
    
    function PrintWebPart()
     {
     var bolWebPartFound = false;
     if (document.getElementById != null)
      {
      var PrintingHTML = '<HTML>\n<HEAD>\n';
      var PrintingtitleHTML = '<HTML>\n<HEAD>\n';
    PrintingHTML += '\n</HEAD>\n<BODY>\n';
    PrintingtitleHTML += '\n</HEAD>\n<BODY>\n';
    var WebPartData = document.getElementById(WebPartElementID);
    var TitlePartData = document.getElementById(WebTitleElementID);
      if (WebPartData != null)
       {
       PrintingHTML += WebPartData.innerHTML;
       PrintingtitleHTML += TitlePartData.innerHTML;
    bolWebPartFound = true;
       }
      else
       {
       bolWebPartFound = false;
       alert ('Cannot Find Web Part');
       }
      }
     PrintingHTML += '\n</BODY>\n</HTML>';
     PrintingtitleHTML += '\n</BODY>\n</HTML>';
    if (bolWebPartFound)
      {
      var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
      PrintingWindow.document.open();
      PrintingWindow.document.write(PrintingtitleHTML,”<BR />”,PrintingHTML);
      // Open Print Window
      PrintingWindow.print();
      }
     }
    }
    </script>
    Posted 1 year ago #
  2. Hi - You haven't stated a problem. What happens when the script runs? What is it supposed to do? A little more detail, please...

    Mark

    PS: We really don't do "code review" in this forum, but since this is javascript, someone might take a shot at it. Don't be disappointed if you don't get a timely response.

    Posted 1 year ago #
  3. malspa
    Member

    Sorry about the lack of detail. I am trying to use the script to create a printable view of a web part and use another web part as the title. I would like both web parts to be in the body of the html so I can edit the font size of the title.

    Posted 1 year ago #
  4. I'm not sure exactly what you mean but I have a script you might be able to work from.

    Add a content editor web part to your page with the following inside:

    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("printWebPart"); 
    
    function printWebPart(wpNum) {
        if (wpNum && document.getElementById != null)    {
            var html = '<HTML>\n<HEAD>\n';
            if (document.getElementsByTagName != null)  {
                var headTags = document.getElementsByTagName("head");
                if (headTags.length > 0) html += headTags[0].innerHTML;
            }
            html += '\n</HEAD>\n<BODY>\n';
            var printDiv = 'WebPartWPQ';
                printDiv += wpNum;
            var printWPElem = document.getElementById(printDiv);
            if (printWPElem != null){
                html += printWPElem.innerHTML;
                }else{
                alert("Could not find the printDiv div");
                return;
            }
            html += '\n</BODY>\n</HTML>';
            var printWP = window.open("","printWebPart");
            printWP.document.open();
            printWP.document.write(html);
            printWP.document.close();
            printWP.print();
        }
    }
    </script>

    Now place a link anywhere you want that looks like this:

    <a href="javascript:void(printWebPart(3))">Print this WebPart</a>

    The number (3) represents the 3rd web part on the page. You have to count the CEWP we added and the default Search Box in the title area. So these two together will print your "main" web part.

    If I have time I might be able to make something that helps "select" the web part. But alas, I have other items to tend to tonight! :)

    Posted 1 year ago #
  5. malspa
    Member

    AutoSponge,

    Thanks for your assistance, you have helped a lot. Do you know if it is possible to print two web parts, that are in two different web part zones, on the same page? Lets say WebPartWPQ3 is a CEWP that displays the custom title of the page and WebPartWPQ5 is a list of phone numbers for my organization. I want to be able to print the phone list on a document with a large title on top.

    Posted 1 year ago #
  6. Sure it's possible. You'll need to either pass another parameter to the script I started with or "predict" the webpart, the way you were doing earlier.

    In this section

    var printWPElem = document.getElementById(printDiv);
            if (printWPElem != null){
                html += printWPElem.innerHTML

    We set the variable printWPElem to our div element then append that to the html variable. You just need to set another element and append again before the /body tag gets appended.

    Posted 1 year ago #
  7. malspa
    Member

    If I understand right, the script would then look like this:

    '<script type=”text/javascript”>
    _spBodyOnLoadFunctionNames.push(“printWebPart”);

    function printWebPart(wpNum)
    {
    if (wpNum && document.getElementById != null)
    {
    var html = ‘<HTML>\n<HEAD>\n’;
    if (document.getElementsByTagName != null)
    {
    var headTags = document.getElementsByTagName(“head”);
    if (headTags.length > 0) html += headTags[0].innerHTML;
    }
    html += ‘\n</HEAD>\n<BODY>\n’;
    var printDiv = ‘WebPartWPQ’;
    printDiv += wpNum;
    var printWPElem = document.getElementById(printDiv);
    var printWPTitl = document.getElementById(‘WebPartWPQ3’);
    if (printWPElem != null)
    {
    html += printWPElem.innerHTML;
    }
    else
    {
    alert(“Could not find the printDiv div”);
    return;
    }
    html += ‘\n’;
    html += printWPTitl.innerHTML;
    html += ‘\n</BODY>\n</HTML>’;
    var printWP = window.open(“”,”printWebPart”);
    printWP.document.open();
    printWP.document.write(html);
    printWP.document.close();
    printWP.print();
    }
    }
    </SCRIPT>'

    As far as I see, this should work, however, when I try to use it, nothing happens.

    Posted 1 year ago #
  8. I cleaned up the single and double quotes, they shouldn't be "curly." And I changed the technique for the anchor to the more appropriate onclick, but yes, this does work:

    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("printWebPart");
    
    function printWebPart(wpNum){
        if (wpNum && document.getElementById != null)
        {
        var html = '<HTML>\n<HEAD>\n';
            if (document.getElementsByTagName != null){
                var headTags = document.getElementsByTagName("head");
                if (headTags.length > 0) html += headTags[0].innerHTML;
            }
        html += '\n</HEAD>\n<BODY>\n';
        var printDiv = 'WebPartWPQ';
        printDiv += wpNum;
        var printWPElem = document.getElementById(printDiv);
        var printWPTitl = document.getElementById('WebPartWPQ3');
            if (printWPElem != null){
                html += printWPElem.innerHTML;
                }else{
                alert("Could not find the printDiv div");
                return;
            }
        html += '\n';
        html += printWPTitl.innerHTML;
        html += '\n</BODY>\n</HTML>';
        var printWP = window.open("","printWebPart");
        printWP.document.open();
        printWP.document.write(html);
        printWP.document.close();
        printWP.print();
        }
    }
    </script>
    <a href="#" onClick="printWebPart(4);">Print this WebPart</a>
    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.