title: Javascript Alert when click close button
body: <p>I have the following code, When I click on close (X) button it shows error stored in variable s. Before the script was working good but now its not showing alert when i click on close button. Did i do any mistake in coding or I need to add some .js file for this to work.</p>
  
  <pre><code>var internalLink = false;
  
  function pageUnload() {
      if (!internalLink &amp;&amp; location.protocol != 'http:') {
          internalLink = true;
          var s = 'Alert Message';
          if (navigator.userAgent.indexOf("Firefox") &gt; -1) {
              alert(s);
          }
          setTimeout("location.href= 'foo.com'", 100);
          return s;
      }
  }
  window.onbeforeunload = pageUnload;
  </code></pre>
  
  <p>Or Can you provide me some other code so that when user clicks on close button. Browser will show alert stored in variable s. Note: Only show alert when click close, Not then when redirects to other link or when submit the form. it should not show any alert when click internal links.</p>