title: How to prevent window.onbeforeunload event being triggered by refreshing the browser
body: <p>I am working on site where i need to remove all the data from the database before user leaves the website and probably i will have to work with the <code>window.onbeforeunload()</code> event i have figure out the problem with links but still i am facing one problem when i refresh the page the event <code>window.onbeforeunload()</code> trigger's and i want only to trigger this function on browser close how can i prevent this function to trigger on the browser refresh. Further the Actions that invoke the onbeforeunload event are following:</p>
  
  <ul>
  <li>Navigating to another page directly in the browser or via a link.</li>
  <li>Closing the current browser window or tab page. Reloading the current</li>
  <li>page. Manipulating the URL of the currently loaded page through the</li>
  <li>location object from JavaScript. Invoking the window.navigate method.</li>
  <li>Invoking the window.open or the document.open method to open a<br>
  document in the same window.</li>
  </ul>
  
  <p>I have figured out all the points except the third one that is Reloading the current page i want this triggers don't work on Reloading the current page .
  Here is my jquery code.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
        $(document).ready(function(){
         $("a").click(function(){
           window.onbeforeunload = null;
         });
      });
  
        var session_id="&lt;?php echo $this-&gt;session-&gt;userdata('session_id');?&gt;";
        var hook = true;
        window.onbeforeunload = function() {
          if (hook) {
  
      $.ajax({
          type:"post",
          url:"&lt;?=base_url();?&gt;temp_delete",
          data:"session_id="+session_id,
          success: function(data)
          {
              alert(data);
              $("#remove").html();
          }
         });
            return "Did you save your stuff?"
          }
        }
  
        function unhook() {
          hook=false;
        }
      &lt;/script&gt;
  </code></pre>