title: Capturing window.onbeforeunload
body: <p>I have a form where the input fields are saved <code>onChange</code>. In Firefox (5) this works even when the window is closed, but for Chrome and IE it doesn't and I need to be sure that I'm saving this data even if they try to close the window after they've typed in a field but an <code>onBlur</code> event hasn't occurred (i.e. they've typed something into a textbox, but haven't tabbed out of it).</p>
  
  <p>I have read the following SO articles on using <code>window.onbeforeunload</code>:
  <a href="https://stackoverflow.com/questions/803887/can-i-pop-up-a-confirmation-dialog-when-the-user-is-closing-the-window-in-safari">article 1</a>
  <a href="https://stackoverflow.com/questions/1244535/alert-when-browser-window-closed-accidentally">article 2</a></p>
  
  <p>if I use the following:</p>
  
  <pre><code>window.onbeforeunload = function() {
      return "onbeforeunload";
  }
  </code></pre>
  
  <p>then I get a popup with <code>onbeforeunload</code> in.</p>
  
  <p>but if I try:</p>
  
  <pre><code>window.onbeforeunload = function() {
      alert("onbeforeunload");
  }
  </code></pre>
  
  <p>then nothing happens in any browser, even Firefox.</p>
  
  <p>what I want to achieve is:</p>
  
  <pre><code>window.onbeforeunload = function() {
      saveFormData();
  }
  </code></pre>
  
  <p>I'd be grateful  if someone could point out where I might be going wrong.</p>