Reputation: 12673
Is it possible to keep displaying the same page even after the user click's the browsers back button?
Its like a multiple-step form which on submit makes a redirect to the resulting page and once the form has been submitted I don't want the user to be able to navigate back to any of the previous pages using browsers back button. So I would like to keep displaying the same resulting page to user on clicking the back button. Just the URL on clicking the back button could change to previous URLs from history but I would not want the user to go back to any of the previous pages.
Could someone suggest me any ideas on how to achieve that if its possible to be done.
EDIT: Say there are 3 steps of some process and then the resulting page like ../step1
, ../step2
, ../step3
, ../resulting page
. User could play with those steps with back and forward button but once the step 3 has been submitted and user is at ../resulting page
I would like the user only to show the URL of the previous displayed page while keeping the result page on.
Upvotes: 0
Views: 2915
Reputation: 51
Yes you can use following javascript code
function backButtonOverride()
{
setTimeout("backButtonOverrideBody()", 1);
}
function backButtonOverrideBody()
{
// Works if we backed up to get here
try
{
window.history.forward(1);
}
catch (e)
{
// OK to ignore
}
// Every quarter-second, try again. The only
// guaranteed method for Opera, Firefox,
// and Safari, which don't always call
// onLoad but *do* resume any timers when
// returning to a page
setTimeout("backButtonOverrideBody()", 1000);
}
if (window.addEventListener) // W3C standard
{
window.addEventListener('load', backButtonOverride, false); // NB **not** 'onload'
}
else if (window.attachEvent) // Microsoft
{
window.attachEvent('onload', backButtonOverride);
}
Upvotes: 5
Reputation: 93444
Whenever you find yourself thinking "I don't want the user to...", you should stop yourself right there. The user is in charge. They should be able to do anything they want to do. Trying to stop them from doing something is akin to telling them what to do.
Instead, you simply have to react appropriately when they do something. In this case, if they use the back button, then you should do something approprate. First, you should design your app so that if they re-request a page after they have completed the step, it does something like redirects them somewhere else.
Second, you need to expire the cache, so that when they go back, the page is expired and will not be re-shown without refreshing the page.
Thus, if the page has been completed, and a previous step is re-requested, you just redirect them back to the appropriate page. You do this typically by setting some kind of unique key in the page so that you can tell that a previously comnpleted page is re-requested.
Upvotes: 3