Detecting a "Back" Button Click
Occasionally, the question comes up about how to detect when
a browser's "back" button is clicked.
With the gaining popularity of form auto-submission blocking
software, the question has become more frequent.
Both the "type what you see in the image" CAPTCHA systems
and the silent detection system employed by
Master Form V4
restrict form submissions to once per page load. If the form
is submitted and the "back" button clicked to submit the
form again, the second submission fails.
When the use of the "back" button can be detected, the form
user can be notified that a reload is necessary before the
form can be used again.
I know of no way to detect the actual click on a browser's
"back" button. However, certain events can be detected (page
load, page unload, page re-focus) and, depending on which
order they occur, determine with some certainty whether or
not the "back" button was clicked.
This JavaScript displays an alert box whenever a "back" button click is spotted.
It's not 100% accurate. For example, a page redisplay from
cache uses the same measured events, in the same order, as a
"back" button click. And JavaScript is required for the
detection to work.
The JavaScript does not confuse a page reload from the
server with the events triggered by a "back" button click --
provided the page load time is correctly specified (see
below). Nor does it confuse an initial page load. The event
sequence is different in those instances.
In the JavaScript, you can specify a different cookie name,
if so desired.
Also, change the number of seconds it takes for your web
page to load. Be a little generous so the JavaScript won't
confuse slow connection reloads with a "back" button click.
As it stands, the JavaScript spawns an alert box when it
detects a "back" button click. That alert can be replaced
with an action or actions consistent with your requirements.
"Back" button click detection is now available.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©Copyright 2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.