title: Javascript How to call a function when we choose Stay on Page in Chrome
body: <p>Please check my code in Chrome Browser, if you hit refresh you will be prompted with 2 options.</p>
  
  <blockquote>
    <ol>
    <li>Leave This Page and</li>
    <li>Stay on This Page</li>
    </ol>
  </blockquote>
  
  <p>When I click the <strong>2. Stay on this page</strong> button it must activate my custom function <strong><em>displayMsg()</em></strong></p>
  
  <p>Can any one provide me solution?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function displayMsg() {
      alert('my text..');
  }
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  window.onbeforeunload = function(evt) {
    var message = 'Please Stay on this page and we will show you a secret text.';
    if (typeof evt == 'undefined') {
        evt = window.event;
    }       
    if (evt) {
        evt.returnValue = message;
        return message;
    }
    trace(evt);
  } 
  &lt;/script&gt;
  </code></pre>