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

Best practice: How to detect when use clicks back button and then refresh page


I'm using Laravel and I'm just wondering what is a best practice for detecting when the user clicks on the back button so that it refreshes the page.  The user is filling out forms and clicking on "Save and Continue" button which uses an Ajax response which saves the data and then goes to the next page. But I notice when I click back that the data isn't being refreshed from the database.

For instance, the user is on page 1 and clicks on checkboxes 1, 3, 5, 10 in a series of checkboxes. Then clicks on the "Save and Continue" button. (Yes, the data is saved correctly). But when the use clicks on the back button those checkboxes are not checked.

I'm wondering if there is a best practice for PHP (Laravel) /Javascript way of handling these things.

BTW, here are some references I've been reading:

Much thanks and Father God bless<><
Victor Kimura
Victor Kimura
2 Solutions
greetings vkimura2007, , , This "Back Button" problem can have very many DIFFERENT changes and effects that depends on the "Page Content" affected, but it's more about what the web developer "Expects or Wants" the browser to show going back, AND there's different views and so called "Solutions" to beat down the browser, to do what the developer wants it to show going back. I always try and see if I can accept the default browser behavior, even if it may be a little inconvenient for the user.

My view for this, is to use a cookie or two, with the "web page ID" for a name-value, for that specific page, in the cookie, and then have a name-value for which check-boxes to set whenever that page is viewed again. But if you need more extensive cross data checking, or data lookups, you can incorporate javascript "local storage", and even Server side database access. As you have seen from some of the pages you gave, there's more than one way to go about this task. .
Short answer - you can't. The back button does not hook into any events that are accessible to Javascript. It's sort of like you might know when someone uses your front door, but you would never know when someone used mine, because you're not at my house. Similarly, the back button is in the browser's house, just like the browser's icon. Those types of things just cannot be accessed or read in any way from Javascript, because Javascript is in the web page's house, and it can only interact with things on the web page itself, like the content on the page, the URL of the page, etc...

There are a few special privileges that the browser does extend to Javascript (to allow Javascript to "click" the back button for the user), but not to detect what the user does.

Usually, if you have scenarios like this, the better thing is to redirect to a confirmation page after AJAX comes back, along with a parameter that has the ID of the record that was saved. Then, that code can pull the record and populate the checkboxes.
Victor KimuraSEO, Web DeveloperAuthor Commented:
Ok, thanks, fellas. @Slick812, yes, I think there are multiple ways to handle this. @gr8gonzo, yes, I see. Good suggestion. =)
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.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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