Asked
Updated
Viewed
40.1k times

On my website, I would like to disable the browser's back button and key so that a user cannot go back to the previous page.

I was taking an online exam in which a message box was displayed when the BACK button was clicked. I don't know if the coding is done in some other language but the message box popped up very quickly as soon I pressed the BACK button.

Additionally, I would like to disable the F5 key to prevent a user from refreshing their browser, perhaps they can be both done in a similar manner using JavaScript?

  • 0
    I imagine you could use a JavaScript:history.go(-1) and then make an alert. — arfinator
  • 0
    It is not a good solution to disable the back key. Why do you think you need to do this? — Mas Sehguh
  • 0
    I have to agree. If you need to disable refresh or the back button, then your site is programmed wrong or you are trying to do something in a browser that would be better off somewhere else (flash or VB). Now, all that aside, I can suggest using JavaScript's location.replace() function to get rid of the back button (only for your site). If you are using strictly HTML and JavaScript, that's the way to go. If you are using PHP, ASP, or CGI to process forms, then you need to rewrite your scripts to handle the back button or refresh instead of trying to prevent them. — ScienceOfSpock
  • 0
    Why do you need to disable F5 and back? There will be a major problem for visitors if you do this. — fyndhern
  • 0
    I would just like to comment. I build a form using HTML and PHP and the form goes through about 9 pages. For some reason, when you click back in IE, it sometimes messes up. OK, I admit, I was on the website clicking back, forward, back, refresh, etc. After about 5 minutes I managed to break it. However, Firefox handles it okay. I think for certain situations such as payment processing or orders, disabling refresh and Back Button could help prevent problems. — onlyican.com
add a comment
0

2 Answers

  • Votes
  • Oldest
  • Latest
Answered
Updated

To disable the back button and another key such as F5 you can use the following code:

<script>
window.history.forward(1); // Disable Back Button
document.attachEvent("onkeydown", my_onkeydown_handler); // Disable F5 Key

function my_onkeydown_handler() {
    switch (event.keyCode) {
        case 116 : // 'F5'
        event.returnValue = false;
        event.keyCode = 0;
        window.status = "We have disabled F5";
        break;
    }
}
</script>
  • 0
    Thanks a lot for the script himansh_raj. I have added it to my HTML pages and it's working. Great job! — mohan
  • 0
    It's very simple to get around this solution by disabling JavaScript in your browser window. You need to validate this on the server side if you hope to make it fool proof. — Prime
add a comment
1
Answered
Updated

Disabling the back button is easy. You need your webpage A to go to a temporary page B that does a JavaScript forward to C. When the user is on page C and hits his back button, it goes back to B, which then goes forward again to C.

The second time through, you could always go to D instead of to C, your choice.

Make some experiments, but it is possible. Also, you might want to investigate using a form POST and JavaScript Submit to disable the back button.

add a comment
0