Send an event to the server when the back button is pressed

Is there any way to send an event to the server when the user hits the back button. I dont need to disable the back button; I just need to know they pressed it.
MikeBroderickAsked:
Who is Participating?
 
DarkoLordCommented:
No. If they are returning (when they press the back button) to your page, you can check the referrer and get their previous site, though.
0
 
DigitalTyrantCommented:
you can use the javascript call within the <body> tag called onbeforeunload.  

Example:
<body onbeforeunload="return notifyServer()">

Then in your scripts file create a function called notifyServer() to do the call to the server.  if you want to prompt the user to stay on your site, you can cancel the action by adding the line:
return 'Are you sure you want to leave?';
and anytime the user attempts to leave your site they will be prompted.

I use this for accidental backspace presses within the forms so that they don't leave and erase the information accidentally.
0
 
MikeBroderickAuthor Commented:
I should have mentioned that this is an ASP.net application. I have used the document.referrer, but sometimes the value contained in document.referrer is not the page that I was on when I hit the back button. I made 3 pages and put next and prev links on them. I put a script on page 2:

function PageInit() {
      var szReferrer = document.referrer.toLowerCase();
      var intPos;
      intPos = szReferrer.indexOf("page3.aspx");
      if (intPos > -1) {
            history.forward(1);
      }
}

<body onload="PageInit()">

The logic all works (I've removed a lot of alert statements. The referrer member will contain Page2.aspx when I came from Page3.aspx. I dont get it.

Is there any request.backbuttonhasbeenpressed field I can test in aspx, or can I test for a document.backbuttonhasbeenpressed field in javascript and send an event that my visualbasic routine can trap?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
MikeBroderickAuthor Commented:
I looked at the onbeforeunload technique. I did not want to bother the user. These backbuttons are all occuring within my pages (if they want to leave thats OK by me). I wanted to try to figure out they pressed the back arrow and could do that if only the client could send an "I just pressed the backarrow" event.
0
 
DarkoLordCommented:
Did you try checking the Referrer field in the server-side (ASP.NET) also?
0
 
DigitalTyrantCommented:
If you return nothing then it will not prompt the user.  That was just an option.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.