title: javascript - Alert when Browser/tab close detection
body: <p>I have this code that alert when I <strong>click a link</strong> or <strong>refresh</strong> or <strong>close tab</strong>.<br>
  But I need alert <strong>only</strong> on <strong>close</strong> window (tab). how to do this?<br> I have many external and internal links on my site.</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript"&gt;
              window.onbeforeunload = function (e) {
                  var e = e || window.event;
                  //IE &amp; Firefox
                  if (e) {
                      e.returnValue = 'Are you sure?';
                  }
                  // For Safari
                   return 'Are you sure?';
              };
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
  
          &lt;!-- this will ask for confirmation: --&gt;
         &lt;!-- I need to alert for external links. --&gt;
          &lt;a href="http://google.com"&gt;external link&lt;/a&gt;
  
  
         &lt;!-- this will ask for confirmation: --&gt;
  &lt;!-- I don't need to alert for local links in my web site --&gt;
              &lt;a href="about.html"&gt;internal link&lt;/a&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>