views:

762

answers:

2

Hi All,

I want a confirmation window on click of a browser back button. If I press yes then the previous page will get load ortherwise I will remain in the same page?

Any suggestion is highly appreciated.. But please be on track.. my question is straight forward

thx in advance..

+2  A: 

Why do you want to do that?

You can prevent them from leaving the page by using Javascript, but if you can't use that, it's not possible to do anything about it.

Generally you should use the unload event in the body (in jQuery for instance, just add

jQuery(window).unload(function(evt){
   if(!confirm('Do you really want to leave')){
      evt.preventDefault();
   }});

Prototype have something similar, and for pure Javascript, I guess that it still depends on the browser you're using, but window.unload = function(evt){return false;} might work.

Don't remember the correct syntax for it though.

but I do not know if you can limit that for only the back or if it will trigger for all the unloads (like clicking on a link, closing the browser etc.)

If you want to stop them because they might have unsaved data in a form, then that is ok. If you want to stop them from going back for another reason than that, I think you should rethink why.

Jimmy Stenke
Hi Keijro,Thx for your quick reply, But i want the confirmation window on clicking of browser back button only, not on unload.So if you have any answer pls suggest.
I am not aware of any way to see if it is precisely the back button that is pressed (maybe by using Firefox and a plugin, but that is really specialized cases). What I would suggest in that case is to have proxy pages that will redirect the user back if it comes from the wrong page (or maybe by using cookies). However, you should really re-think the reason for why you don't want to allow them to go backwards. Perhaps there is a better solution.
Jimmy Stenke
A: 

Generally if using the back button can cause issues you already have bigger problems.

What you probably want to do is check that you do things like this:

  • Use POST for all requests that alter data
  • Use nonce's (unique ID's) to enure forms don't get submitted twice
LapTop006
+1 to undo drive-by -1. I think this is a reasonable response (albeit it isn't an answer to the question). Despite the request to answer the question asked, I think it is unrealistic to let someone think they can control my web browser to the point of preventing me from clicking the back button.
Grant Wagner