title: Bypass Chrome Popup on Browser Window Close
body: <p>In a Chat application that I am developing, I must call a function that cleans up connections when the User closes the Chat Window. The Chat Window is in a popup browser. ("closes" meaning the user presses the red 'x' of the window).</p>
  
  <p>I have been able to detect the close event and call the function simply in IE11 using the onbeforeunload event handler.</p>
  
  <p>IE11 Code:</p>
  
  <pre><code> window.onbeforeunload = function() {             
      cleanUp();
   };
  </code></pre>
  
  <p>However, now I must do the same for Chrome, but there seems to be a "Leave this Page/Stay on this Page" popup that occurs when the User tries to exit (that is, with my beforeunload handler in place).
  I would rather the user could exit cleanly and not be interrupted by this message. </p>
  
  <p>Furthermore, my function is not getting called in my handler in Chrome. I'm not sure why.</p>
  
  <p>Is there a way to bypass the popup message in Chrome, or is this considered bad practice? Also, how do I ensure my function gets called, upon window close?</p>
  
  <p>I have seen the following being used to detect the close event in Chrome:</p>
  
  <p>Chrome Code:</p>
  
  <pre><code>var unloadEvent = function (e) {
      var confirmationMessage = "Are you sure you want to leave?";
      (e || window.event).returnValue = confirmationMessage;
      return confirmationMessage;
  };
  
  window.addEventListener("beforeunload", unloadEvent);
  </code></pre>
  
  <p>And I have tried modifying it to call my function like so:</p>
  
  <p>Chrome Code Modified:</p>
  
  <pre><code>var unloadEvent = function (e) {
      var confirmationMessage = "Are you sure you want to leave?";
      (e || window.event).returnValue = confirmationMessage;
      return cleanUp(confirmationMessage);
  };
  
  window.addEventListener("beforeunload", unloadEvent);
  </code></pre>
  
  <p>Note: I read that the beforeunload handling here in Chrome expects a return String, so I call my cleanUp function to return the confirmationMessage String from there, while also calling my clean up code.</p>
  
  <p>Any help appreciated.</p>
  
  <p>Thanks,
  Dearg</p>