title: How do I wait for location.href to finish in JavaScript?
body: <p>The JavaScript below code has two steps.  </p>
  
  <p><strong>Step 1</strong>: go to .pdf, .doc, .exe or something which not html native.  If location.href has took over the browser window then there is no need to do step 2.  (PDFs usually take over browser window).  Most other things kick off a download manager process.  Such as .exe.  But there are some things such as word docs that could be either a download or show right in the browser window depending on browser setup.  I want it to do what hef.location would make it do.</p>
  
  <p><strong>Step 2</strong>:  But if it is downloading a file such as an .exe after that process is done then go to home page.</p>
  
  <p>Or solution for just waiting 5 seconds between steps 1 and 2 seem to work most of the time.  But on a slower connection it doesn't always works.  Then it goes to home page without finishing the first href.location call and they never see the PDF and just see home page.  </p>
  
  <p>FYI...The reason I am wrapping them in setTimeOut is related to this firefox issue.  <a href="https://stackoverflow.com/questions/864633/assigning-to-document-location-href-without-clobbering-history">Stack Overflow: 864633 assigning-to-document-location-href-without-clobbering-history</a></p>
  
  <p><strong>My question:</strong>  Is there a way to tell when the location.href process gets completed?</p>
  
  <pre><code>&lt;script language="JavaScript"&gt;&lt;!--
  function windowOnLoad() {
      setTimeout(function(){
         location.href='/someurl/something.pdf';  //sometimes this is .doc file
      },0);
      setTimeout(function(){
         location.href='/homepage';
      },5000);
      return false;
  }
  //--&gt;
  &lt;/script&gt;
  </code></pre>