trapping back button breaks button on other pages

trapping back button breaks button on other pages

Hi All!
 
I want to trap the back button on my index.html page so that users will know when they're exiting the app vs sending the app to the background.
 
I have code that works by trapping the back button, checks the page and if it's the index page, prompts to exit and cancels the exit if necessary.  The problem is, when I navigate to another page the back button no longer works at all.
 
At this point in the development, this is a multiple page model so each page is in its own seperate file and to me, document.addEventListner should terminate when a new page (document is loaded).
 
Here's the code:
  1. document.addEventListener(" backbutton", function(e) {
  2. if($('. ui-page-active'). attr('id') == "main");
  3. {
  4.   if(confirm("Really exit blah blah?\n\nIf you do, you will no longer receive messages!") )
  5.   {
  6.     device.exitApp();
  7.   }
  8.   else
  9.   {
  10.     navigator.app.backHistory();
  11.   }
  12. }
  13. navigator.app.backHistory();
  14. }, false);