title: Prompt User before browser close?
body: <p>We have an administrative portal that our teachers constantly forget to download their latest PDF instructions before logging out and/or closing the browser window. I have looked around but can't find what I'm looking for. </p>
  
  <p>I want to accomplish the following goals:</p>
  
  <h2>Goal 1</h2>
  
  <p>Before a user can close the browser window, they are prompted "Did you remember to download your form?" with two options, yes/no. If yes, close, if no, return to page.</p>
  
  <h2>Goal 2</h2>
  
  <p>Before a user can click the 'logout' button, they are prompted with the same as above.</p>
  
  <p>My first pass at the very basic code (which does not work for browser close) is:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  
  &lt;script type="text/javascript"&gt;
  function init() {
      if (window.addEventListener) {
          window.addEventListener("beforeunload", unloadMess, false);
      } else if (window.onbeforeunload) {
          window.onbeforeunload = unloadMess;
      };
  }
  
  function unloadMess() {
      var User_Message = "[Your user message here]"
      return User_Message;
  }
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body onload="init();"&gt;
       hello this is my site
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>EDIT - NEW ISSUES</h2>
  
  <p>The solution provided below works but also has its own issues:</p>
  
  <p>When user clicks the link to actually download the forms, the page thinks they are trying to leave and in turn present an error message to them! Also - I would like for one event or another to occur but not necessarily both. I.e. they hit 'logout' button and they are presented with the OnClick event, THEN the browser prompt. Any ideas?</p>