Message on clicking browser back button

S

srraajesh

Hi,

Is it possible to display a message using Javascript on click of
"browser back button". If so, can somebody post a code snippet of
that?

Thanks..
 
S

SAM

(e-mail address removed) a écrit :
Hi,

Is it possible to display a message using Javascript on click of
"browser back button". If so, can somebody post a code snippet of
that?

<body onunload="alert('Ho poor ! you\'re leaving me ?');">

<body onbeforeunload="return false">
 
T

T.J. Crowder

Hi,

Lots of results via Google:

http://www.google.com/search?q=disabling+the+back+button

AFAIK, you can't actually do this. If you absolutely, positively want
to do this, there are several articles in the search above that talk
about how to come close.

The question I think you probably want to ask yourself, though, is why
do you want to? E.g., step back from the technical problem for a
moment and consider the design problem. One of main powers of the web
is that pages are addressable; the back and forward buttons are really
just a side-effect of that, but such a useful one that the web has
even influenced file managers like Windows Explorer (surely not a
bastion of innovation!) to add forward and back buttons. Most likely,
your users *want* to be able to go backward and forward through your
site.

Frequently, people want to do this because they want to avoid having a
form get reposted when the user clicks the Back button after posting.
There are a couple of better ways to do that: Using an XHR to send
the form data instead of actually posting the form (if you're limiting
yourself to browsers with XHR), or using the redirect-after-post
pattern (for more limited browsers):
http://www.google.com/search?q=redirect+after+post
http://en.wikipedia.org/wiki/Post/Redirect/Get

The nice thing about redirect-after-post is that most of the time it
lets your users do what they *meant* to do when they clicked the Back
button. Which is really my point: Barring some edge cases (and
perhaps yours is one of them), with proper design you can support the
back button rather than prohibiting it.

FWIW,
 
T

T.J. Crowder

Blast. You said "display a message", not "disable."

Need more coffee.

Apologies!
 
B

Bart Van der Donck

SAM said:
(e-mail address removed) a écrit :

<body onunload="alert('Ho poor ! you\'re leaving me ?');">

<body onbeforeunload="return false">

Whilst this may work when hitting the back-button, it also shows the
message when hitting the forward-button, home-button, typing a new
URL, visiting a web site from Favourites, etc.

Typical DWIM task :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top