Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1810 How can I disable the back button?

You are here: irt.org | FAQ | JavaScript | History | Q1810 [ previous next ]

This method is useful if you don't want the user to go back because your cgi will repeat an event that you don't want.

It uses a form element to set a flag, which is checked when the page loads. The page is a hidden, self-submitting page and is placed immediately after the page you don't want the user to go back to.

The first time the user goes through, the value of the flag is changed. This value persists and remains changed if the user tries to go back to the page. Javascript checks the value, and it will be wrong, so an optional alert is displayed and the user is sent forward to the page he is supposed to be on.

<SCRIPT>
function checkFlag() {
  with (document.forms[0]) {
    if (flag.value == "1") {
      flag.value = 0;
      submit();
    } else {
      alert('You cannot go back!');
      history.go(1);
    }
  }
}
</SCRIPT>

</head>
<BODY onLoad="checkFlag()">
<FORM ACTION="some cgi">
<INPUT TYPE=HIDDEN NAME=flag VALUE="1">
</FORM>
</BODY>

Submitted by Tim Webster

©2018 Martin Webb