• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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.
0
MikeBroderick
Asked:
MikeBroderick
  • 2
  • 2
  • 2
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now