append image javascript occasionally fails
<p>I am using the below script to format a special print window from my sharepoint site.</p>

<p>The problem is sometimes the very last statement in the script (the picture append) does not load. Is there a way to catch if it fails and force the script to reload ?</p>

<p>I have tried loading the picture directly in the printing html but it seems to not work.</p>

<p>I am not very experienced with javascript, any suggestions of improvement are very welcome</p>

<pre><code>&lt;center&gt; 
   &lt;input onclick="PrintWebPart();" type="button" value="Print Proof of Pricing"/&gt;  
&lt;/center&gt;
&lt;script language="JavaScript"&gt;
//Controls which Web Part or zone to print
var WebPartElementID = "ctl00_ctl34_g_db6615a7_4c3b_4a14_9bbc_43ce9d63c24d_FormControl0";
var d= new Date();


//Function to print Web Part
function PrintWebPart() {
  var bolWebPartFound = false;
  if (document.getElementById != null) {
    //Create html to print in new window
    var PrintingHTML = '&lt;HTML&gt;\n&lt;HEAD&gt;\n';
    //Take data from Head Tag
    if (document.getElementsByTagName != null) {
      var HeadData= document.getElementsByTagName("HEAD");
      if (HeadData.length &gt; 0) {
        PrintingHTML += HeadData[0].innerHTML;
      }
    }
    PrintingHTML += '\n&lt;/HEAD&gt;\n&lt;BODY&gt;\n';
    var WebPartData = document.getElementById(WebPartElementID);
    if (WebPartData != null) {
      PrintingHTML +='&lt;div id="imageDiv" align="center"&gt;\n&lt;/div&gt;\n'
      PrintingHTML += WebPartData.innerHTML;
      PrintingHTML += "Pricing Date: " +d;
      bolWebPartFound = true;
    } else {
      bolWebPartFound = false;
      alert ('Cannot Find Web Part');
    }
  }
  PrintingHTML += '\n&lt;/BODY&gt;\n&lt;/HTML&gt;';
  //Open new window to print
  if (bolWebPartFound) {
    var PrintingWindow = window.open("","Print_Proof_of_Pricing", "toolbar= yes,width=800,height=600,scrollbars,resizable,menubar");
    PrintingWindow.document.open();
    PrintingWindow.document.write(PrintingHTML);
    PrintingWindow.document.getElementById('imageDiv').innerHTML = '&lt;img src="Some_url" alt="Title" height="170" width="300"/&gt;';
  }
}
&lt;/script&gt;
</code></pre>