title: Javascript - Confirmation when leaving page
body: <p>I'm trying to implement a basic popup window that asks the user if they really want to leave a page, similar to what would happen on this site if I tried to close the window half way through writing this message.</p>
  
  <p>I realise this is something that is generally frowned upon but I have good reason for wanting to do it.</p>
  
  <p>I have got it working by using the following code:</p>
  
  <pre><code>function confirm_exit(e) {
          if(!e) e = window.event;
  
          e.cancelBubble = true;
          e.returnValue = 'Are you sure you want to leave?'; 
  
          if (e.stopPropagation) {
              e.stopPropagation();
              e.preventDefault();
          }
      }
  </code></pre>
  
  <p>However, what I would really like to do is display the message whenever they leave the page, UNLESS they leave by clicking one of two links.</p>
  
  <p>(I've just realised that sounds like I might want to force them to click an advert or something!)</p>
  
  <p>The reason for using this is at the end of a booking process, where users can either confirm their booking or add further bookings before making the confirmation. (These would be the two possibilities that I would NOT like the popup message to display, the message in the pop up would just say something like 'Your booking is not yet confirmed, are you sure you wish to leave?).</p>
  
  <p>Is there anyway to achieve this?</p>
  
  <p>Any advice appreciated.</p>
  
  <p>Thanks.</p>