title: I need to capture the window.onbeforeunload event
body: <p>I am working on a project where I need to capture the browser close event (<code>unload</code> or <code>beforeunload</code>). For that I have tried the code below but it's not working.  </p>
  
  <p>The code is working only if I open the browser console window (maybe just for a second) but it is required otherwise it's not working.</p>
  
  <p><a href="https://www.w3schools.com/code/tryit.asp?filename=G9OOMQHCZNVN" rel="nofollow noreferrer">Example</a></p>
  
  <pre><code>$(window).on('beforeunload', function(e) {
   $.ajax({
      type: "POST",
      url: "url",
      data : {'value' : 1},
      dataType:'json'
   });
    return false;   
  });
  </code></pre>