Today I ran into a similar problem, and I solved it using localStorage (here with a bit of jQuery):
$(function() { if ($('#page1').length) { // this code must only run on page 1 var checkSteps = function () { if (localStorage.getItem('steps') == 'step2') { // if we are here, we know that: // 1. the user is on page 1 // 2. he has been on page 2 // 3. this function is running, which means the user has submitted the form // 4. but steps == step2, which is impossible if the user has *just* submitted the form // therefore we know that he has come back, probably using the back button of his browser alert('oh hey, welcome back!'); } else { setTimeout(checkSteps, 100); } }; $('#form').on('submit', function (e) { e.preventDefault(); localStorage.setItem('steps', 'step1'); // if "step1", then we know the user has submitted the form checkOrderSteps(); // ... then do what you need to submit the form and load page 2 }); } if ($('#page2').length) { // this code must only run on page 2 localStorage.setItem('steps', 'step2'); } });
Si mainly:
On page 1, when the user submits the form, we set the value of "steps" to localStorage to indicate which step the user took. At the same time, we run a function with a timeout, which checks whether this value has been changed (for example, 10 times / sec).
On page 2, we will immediately change the indicated value.
Therefore, if the user uses the "Back" button, and the browser restores page 1 to the exact state when we left it, the checkSteps function still works and can detect that the value in localStorage has been changed (and may take appropriate measures). Once this check fills its target, there is no need to continue to run it, so we just no longer use setTimeout.
s427 Jun 08 '16 at 15:29 2016-06-08 15:29
source share