[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 994
  • Last Modified:

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

Hi,

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:
http://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser
http://stackoverflow.com/questions/2008806/how-to-detect-if-the-user-clicked-the-back-button
http://www.webdeveloper.com/forum/showthread.php?137518-How-to-refresh-page-after-clicking-quot-Back-quot-button
https://www.mnot.net/cache_docs/
https://www.udemy.com/dont-break-the-back-button-the-html5-history-api/
http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser
http://www.javascriptsource.com/buttons/refresh-button.html

Much thanks and Father God bless<><
0
Victor Kimura
Asked:
Victor Kimura
2 Solutions
 
Slick812Commented:
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. .
0
 
gr8gonzoConsultantCommented:
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.
0
 
Victor KimuraAuthor Commented:
Ok, thanks, fellas. @Slick812, yes, I think there are multiple ways to handle this. @gr8gonzo, yes, I see. Good suggestion. =)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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