title: How to call a function before leaving page with Javascript
body: <p>I would like to execute a function before leaving page without showing a confirmation popup with <strong>Javascript</strong> only. I've tried with the code below but it didn't work or with the <code>onbeforeunload</code> but it always shows the popup.</p>
  
  <pre><code>var result = 'test';
  
  if(window.onbeforeunload == true)
  {
      result = 'test1';
      alertmess();
  }
  
  function alertmess() {
      alert(result);
  }
  
  //window.onbeforeunload = function() { 
  //  return result; 
  //}
  </code></pre>