title: Trigger an event when the browser back button is clicked
body: <p>I am trying to run some code when the browser back button is clicked.</p>
  
  <p>How can i found out browser's back button with out changing the browser history?</p>
  
  <p>I tried the code below.
   I got an exception in the <code>else</code> block saying: "event is not defined".        </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onunload = HandleBackFunctionality();
    function HandleBackFunctionality()
    {
      if(window.event)
      {
        if(window.event.clientX &lt; 40 &amp;&amp; window.event.clientY &lt; 0)
        {
          alert("Browser back button is clicked…");
        } else {
          alert("Browser refresh button is clicked…");
        }
      } else {
        if(event.currentTarget.performance.navigation.type == 1)
        {
          alert("Browser refresh button is clicked…");
        }
        if(event.currentTarget.performance.navigation.type == 2)
        {
          alert("Browser back button is clicked…");
        }
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>