How to disable Back button

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How to disable Back button

Post by simonmlewis »

Hi

I have a page that runs a query, then redirects (meta redirect) to another page, but I don't want the user to be able to 'go back' by clicking the BACK button on the browser.

A crude was to stop it is to redirect them to three pages (as backing backing backing.... with those pages redirecting forward), but it's a pain.

Can you just switch off the back button on the page itself? I was going to use this:
<a href="next.html" onclick="location.replace(this.href);return false;">TEST</a>
But the redirect cannot use this code - can it ?? If I can use this in a redirect:
echo "<meta http-equiv='Refresh' content='0 ;URL=index.php?page=vouchercomplete&email=$email&vc=$code'>";
then I will - but how please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: How to disable Back button

Post by PHPHorizons »

Hello simonmlewis,

You cannot disable the back button. Even if javascript could do that, the user can simply turn off javascript, or may have it turned off already.

The way to do this is to use window.location.replace('new_web_page.html'). That method will *replace* the current page in the history. Therefore, if they hit the back button, they go to the page *before* the form submit handler.

This also has the deficiency that if javascript is turned off, it will not work. But it benefits from not taking over the user's browser and disabling their back button.

Cheers :)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to disable Back button

Post by simonmlewis »

Sorry I don't understand how that works.
How do you input that into the code after the script is run?

Does it prevent them hitting 'backspace' and running a script on the previous page again?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: How to disable Back button

Post by PHPHorizons »

To understand this, you need to understand the concept of a browser history (i.e., when you hit the back button, you go to the previous page in the history). By replacing the form submit handler page in the history, one cannot go back to that page. You go to the page before that page.

The javascript code to do this is exactly as I outlined it: window.location.replace('new_web_page.html')

You need to replace new_web_page.html with the page you want the user to land on.

Cheers
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to disable Back button

Post by simonmlewis »

ie
<script>
window.location.replace('apage.html')
</script>
to be shown on the resulting page?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: How to disable Back button

Post by PHPHorizons »

Yeah ;)

Let me know how that works out for ya.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to disable Back button

Post by simonmlewis »

Wow that's a little bit of genius!!!
Works brill.

Means someone can claim a voucher, but when I hit BACK, it just goes back to the Claim form.

Lovely. Thx.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: How to disable Back button

Post by PHPHorizons »

Hehe, wish I could claim that I invented it! But I am glad to be able to pass it on.

Cheers ;)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How to disable Back button

Post by simonmlewis »

This issue I had before is now extended because of something else: what if I have POST data to that page, and then on Click next, and on the following page, I click BACK - I then have the issue of "Document Expired".
How do I capture in PHP if the page has been loaded, but there data expected is not there?
The "Next" button on the first POST page, is triggered if $calculated != NULL.

If it IS NULL, then it doesn't show 'Next'. But if it is, it shows the normal 'Calculate' box.

So how do I get around this? Without totally recoding it all to pass through things as a GET, meaning (I think) I have to change the URL.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How to disable Back button

Post by Weirdan »

The standard way to handle post is Post/Redirect/Get.
Post Reply