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

Posted on 2007-07-26
Last Modified: 2008-01-09
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.
Question by:MikeBroderick
    LVL 22

    Accepted Solution

    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.
    LVL 4

    Assisted Solution

    you can use the javascript call within the <body> tag called onbeforeunload.  

    <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.

    Author Comment

    I should have mentioned that this is an 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) {

    <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?

    Author Comment

    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.
    LVL 22

    Expert Comment

    Did you try checking the Referrer field in the server-side (ASP.NET) also?
    LVL 4

    Expert Comment

    If you return nothing then it will not prompt the user.  That was just an option.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now