title: Javascript To Get An Alert To Redirect When Closing The Browser Window
body: <p>I'm trying to get this Javascript to work properly. My intention is, when a user is trying to close the site page to get an alert saying "stay on current page or close" If they hit 'ok' I want it to close, if they hit 'cancel' i want it to redirect to another page. The problem is, when they try to go to another page on the same site, it gives them that popup. I want it to show only when closing, not when leaving the page to another page. I'm not sure if that's possible, I do appreciate your help and comments.</p>
  
  <pre><code>    window.onbeforeunload = fget;
  function yPop(url) {
    var found = 1;
    window.onbeforeunload = '';
    window.location = url;
    return false;
  }
  function fget() {
    alert("Thank you for visiting our website, you are welcome any time! \n\n");
    window.location = "http://NewLink.com";
    return "\n________________________\n\n PRESS 'CANCEL' To Stay On The Current Page \n\n________________________\n";
  }
  </code></pre>