Q: Controlling the Back button using onhashchange or jquery I need some guidance on how to have very basic control in the use of the back button.
Basically, I need to warn the user that by clicking the back button, when on my checkout.asp page, they will lose the items already there.  I need to instruct them to use the navigation buttons instead.
I've had a look around a have seen mention of the onhashchange event but I could not get it to work.
I also tried the plugin by Ben Alman:
<script type="text/javascript" src="jquery/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="jquery/js/jquery.ba-hashchange.js"></script>

<script language="JavaScript" type="text/JavaScript">
$(function(){
    // Bind the event.
    $(window).hashchange( function(){
    // Alerts every time the hash changes!
    alert( location.hash );
    })
    // Trigger the event (useful on page load).
    $(window).hashchange();
});
</script>

This only fires up the alert (with no value from the location.hash) when entering the page but I simply want to warn the user if they're leaving. If they choose to stay then let them press cancel to leave them on the same page.
Any help appreciated.

A: Use:
window.onbeforeunload = function() {
   return confirm("you have unsaved data. ok to exit?")
})


A: In order to only show a warning when the back button is used, we have to eliminate the alert message when a valid source is clicked.
var showWarning = true;

window.onbeforeunload = function() {
  if (showWarning) {
    return confirm("you have unsaved data. ok to exit?");
  }
}

function clearWarning() {
  showWarning = false;
} 

Now just make sure to call the clearWarning() function when an allowed button is clicked. Something like this should work:
referenceToElement.addEventListener('onClick', clearWarning(), false);

I have not tested this, but I would imagine that something like this is what you need.
