title: top.document.location.href property behaviour in IE(8/9)
body: <p>I have a piece of code which works in a way that is unexpected to me. Let me first put down the details, so the code looks more or less like this:</p>
  <pre><code>window.onunload = doUnload;
  var unLoadTimer;
  var gInvokeOnBeforeUnload = true;
  function setInvokeOnBeforeUnload() { gInvokeOnBeforeUnload = false; }
  
  function doUnload() {
  if(gInvokeOnBeforeUnload==true){
      setInvokeOnBeforeUnload();
      top.document.location.href = &quot;LogOff&quot;;
      clearTimeout(unLoadTimer); 
  }
  //Function Midpoint
  if (top.document.readyState != &quot;complete&quot; &amp;&amp; gLogOffClick == false){
      unLoadTimer = setTimeout(&quot;doRealUnload(&quot;+func_call_count+&quot;)&quot;, 800); 
      //its just a timeout for the function to be called again
  }
  }
  </code></pre>
  <p>How I also added logging but for sake of keeping code small excluded from examples. The issue I faced was that <code>top.document.location.href</code> is NOT reassigned to &quot;LogOff&quot; and is still equal to the very first starting page I opened on the site, call it &quot;LogIn&quot;.</p>
  <p>To trigger the <code>window.onunload </code>event I hit <kbd>F5</kbd> (and it happens all the time), but the logoff is not assigned to <code>location.href</code>. Only if at &quot;//Function Midpoint&quot; comment marker I add a simple <code>alert(1);</code>  then logoff is triggered (as seen in Apache logs below), however location href is still the same as it was - &quot;LogIn&quot;.
  Am I wrong to assume it should change as soon as its assigned a value?</p>
  <p>And so I'm failing to understand several points:</p>
  <ol>
  <li>Why <code>location.href</code> is not changed as soon as I assign it the value of LOGOFF.</li>
  <li>What should happen as soon as <code>LOCATION.HREF</code> is changed</li>
  <li>How does <code>LOCATION.HREF</code> get initialized when I open the browser and logon</li>
  </ol>
  <p>Don't have any fancy session tracking mechanisms, its a straight forward HTML page with the above JS triggered @ <code>onunload</code> via <kbd>F5</kbd>.</p>
  <p>All I want to achieve is to understand what happens behind the scenes and why the redirect happens if there is an alert(1); inbetween and doesn't if no break is there. In my case Apache registered the following:</p>
  <p>With timeout:</p>
  <pre><code>IP.IP.IP.IP - xxx [28/Oct/2011:10:38:50] &quot;GET /LogOff HTTP/1.1&quot; 200 208
  IP.IP.IP.IP - xxx [28/Oct/2011:10:38:50] &quot;GET /LogIn HTTP/1.1&quot; 401 540
  IP.IP.IP.IP - xxx [28/Oct/2011:10:38:52] &quot;GET /LogIn HTTP/1.1&quot; 200 2898
  </code></pre>
  <p>Without timeout:</p>
  <pre><code>IP.IP.IP.IP - xxx [28/Oct/2011:10:41:04] &quot;GET /LogIn HTTP/1.1&quot; 200 2726
  </code></pre>
  <p>Any help or guidance towards what to read/where to look is appreciated, even better if someone could elaborate what happens behind the scenes. Im afraid I'm not well educated in JS engines, to my disadvantage. The code is also not really mine :(</p>
  <p>If you have extra question I'm listening, maybe I omitted something important there :)</p>
  <p>Didn't try to test in other browsers as IE is the only focus group I need...</p>