title: Detect if a tab/download has finished loading in JavaScript
body: <p>I'm generating a pdf on Grails using the following pattern:</p>
  
  <blockquote>
    <p><strong>Optional Step:</strong>
    The user will choose either he wants to download the *.pdf or to open it on another tab, the default is to open it to another tab.</p>
  </blockquote>
  
  <p><strong>Main Step:</strong></p>
  
  <ol>
  <li>Click the <kbd>Generate Report</kbd> button.</li>
  <li>The button will change its text to <kbd>Generating Report, please
  wait...</kbd></li>
  <li><p>It will run a block of JavaScript something like this:</p>
  
  <pre><code>var generate = "&lt;sample url&gt;"
  
  // some amazing processing goes here that appends data to the
  // query string.
  
  window.open(generate,'_blank');
  </code></pre></li>
  <li><p>Then it will open a new tab (or start downloading, depending on the option he had chosen).</p>
  
  <blockquote>
    <p>Note: The opened tab is not an ordinary html markup, but rather a <code>*.pdf</code> that is rendered on a browser tab.</p>
  </blockquote></li>
  <li>The tab (or the download) will load for at least five seconds to 1 minute or so.</li>
  <li>The pdf on that tab will finish loading and the user can now navigate the pdf (scroll down, save, print etc); or if it is a download, he can now open that file.</li>
  <li>The button on the calling tab will change its text to <kbd>Report
  has been generated</kbd>.</li>
  </ol>
  
  <p>I already generated the reports successfully (both the new tab and the download approach), however my problem is <em>I don't know how to detect if that tab has finished loading</em>, and I cannot do step 7. Can anyone help me how to implement this?</p>