Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 484427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:17:54+00:00 2026-05-13T01:17:54+00:00

I have an AJAX app. A user clicks a button, and the page’s display

  • 0

I have an AJAX app. A user clicks a button, and the page’s display changes. They click the back button, expecting to go to the original state, but instead, they go to the previous page in their browser.

How can I intercept and re-assign the back button event? I’ve looked into libraries like RSH (which I couldn’t get to work…), and I’ve heard that using the hash tag somehow helps, but I can’t make sense of it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:17:55+00:00Added an answer on May 13, 2026 at 1:17 am

    Ah, the back button. You might imagine "back" fires a JavaScript event which you could simply cancel like so:

    // this does not work
    document.onHistoryGo = function() { return false; }
    

    No so. There simply is no such event.

    If you really do have a web app (as opposed to just a web site with some ajaxy features) it’s reasonable to take over the back button (with fragments on the URL, as you mention). Gmail does this. I’m talking about when your web app in all in one page, all self-contained.

    The technique is simple — whenever the user takes action that modifies things, redirect to the same URL you’re already on, but with a different hash fragment. E.g.

    window.location.hash = "#deleted_something";
    ...
    window.location.hash = "#did_something_else";
    

    If the overall state of your web app is hashable, this is a great place to use a hash. Say you have a list of emails, maybe you’d concatenate all their IDs and read/unread statuses, and take an MD5 hash, using that as your fragment identifier.

    This kind of redirect (hash only) doesn’t try to fetch anything from the server, but it does insert a slot in the browser’s history list. So in the example above, user hits "back" and they’re now showing #deleted_something in the address bar. They hit back again and they’re still on your page but with no hash. Then back again and they actually go back, to wherever they came from.

    Now the hard part though, having your JavaScript detect when the user hit back (so you can revert state). All you do is watch the window location and see when it changes. With polling. (I know, yuck, polling. Well, there’s nothing better cross-browser right now). You won’t be able to tell if they went forward or back though, so you’ll have to get creative with your hash identifiers. (Perhaps a hash concatenated with a sequence number…)

    This is the gist of the code. (This is also how the jQuery History plugin works.)

    var hash = window.location.hash;
    setInterval(function(){
        if (window.location.hash != hash) {
            hash = window.location.hash;
            alert("User went back or forward to application state represented by " + hash);
        }
    }, 100);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

net ajax app. I have one modelpopup that shows a IFrame and inside Iframe
Once my Rails 3 app is loaded, a user can click an item which
Rails 3 app.... I have the following jQuery which is working: $.ajax({ url: '/navigations/sidenav',
I have an approach in mind for an image viewer in a web app,
So everything works. But if a user has firebug's console open, they'll see a
I have a RoR app running rails 2.3.8 which makes use of the Rails
I'm developing this app which as a very basic Ajax form and I'm currently
Goal I want to create a web app with a horizontal GUI bar that
I was hoping for a little help. I am currently using some of the
ok, rails 3 new developer here. I want my jquery to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.