title: Why onbeforeunload event is not firing
body: <p>I tried the code on Chrome, FireFox and Safari. Still the <code>onbeforeunload</code> does not fire. I also tried the <code>onunload</code>, but it did not work.</p>
  
  <p>Here is the code I am experimenting with:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;title&gt; Output to a Page &lt;/title&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;script&gt; 
      window.onload = init;
      window.onclick = clk;
      window.onbeforeunload = closing;
      function clk() {
          window.alert("Ouch !!");
      }
      function closing() {
          console.log("function alrt WORKS !!!!");
          window.alert("closing now.....");
      }
      function init() {  
        var output = "";
        for (var mms = 5; mms &gt; 0; mms--) {
          if (mms &gt;= 3) {
              output += "Still lots of M&amp;amp;Ms left, so eat more!&lt;br&gt;";
          } else {
              output += "Getting low on M&amp;amp;Ms, take it easy!&lt;br&gt;";
          }
        }
        output += "All out of M&amp;amp;Ms";
        var e = document.getElementById("output");
        e.innerHTML = output;
      }
  
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;  
    &lt;p id="output"&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>