Solved

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

Posted on 2015-01-20
3
510 Views
Last Modified: 2015-02-04
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
Comment
Question by:Victor Kimura
3 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
Comment Utility
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
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
Comment Utility
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
 

Author Closing Comment

by:Victor Kimura
Comment Utility
Ok, thanks, fellas. @Slick812, yes, I think there are multiple ways to handle this. @gr8gonzo, yes, I see. Good suggestion. =)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now