Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2015-01-20
Medium Priority
Last Modified: 2015-02-04

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<><
Question by:Victor Kimura
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 34

Accepted Solution

Slick812 earned 1000 total points
ID: 40562190
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. .
LVL 35

Assisted Solution

gr8gonzo earned 1000 total points
ID: 40562208
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.

Author Closing Comment

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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

636 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