title: using onbeforeunload event, url change on selecting stay on this page
body: <p>Rewriting the question - </p>
  
  <p>I am trying to make a page on which if user leave the page (either to other link/website or closing window/tab) I want to show the <code>onbeforeunload</code> handeler saying <code>we have a great offer for you?</code> and if user choose to <code>leave the page</code> it should do the normal propogation but if he choose to <code>stay on the page</code> I need him to redirect it to offer page <code>redirection is important, no compromise</code>. For testing lets redirect to <code>google.com</code></p>
  
  <p>I made a program as follows - </p>
  
  <pre><code>var stayonthis = true;
    var a;
    function load() {
     window.onbeforeunload = function(e) {
          if(stayonthis){
           a = setTimeout('window.location.href="http://google.com";',100);
           stayonthis = false;    
           return "Do you really want to leave now?";
          }
          else {
              clearTimeout(a);
          }
  
      };
      window.onunload = function(e) {
           clearTimeout(a);
      };
    }
    window.onload = load;
  </code></pre>
  
  <p>but the problem is that if he click on the link to <code>yahoo.com</code> and choose to <code>leave the page</code> he is not going to yahoo but to google instead :(</p>
  
  <p>Help Me !! Thanks in Advance</p>
  
  <p><a href="http://jsfiddle.net/pb7th/" rel="noreferrer"><strong>here is the fiddle code</strong></a> </p>
  
  <p><a href="http://fiddle.jshell.net/pb7th/show/" rel="noreferrer"><strong>here how you can test because onbeforeunload does not work on iframe well</strong></a> </p>