How to manage a browser button

I want a confirmation box when the back button is clicked. If I click yes, will the previous page get the load or will I stay on the same page otherwise?

Any suggestion is greatly appreciated. But please be on your way. My question is straightforward.

thanks in advance..

0


a source to share


3 answers


Why would you want to do that?

You can prevent them from leaving the page using Javascript, but if you can't use that, then there's nothing to be done.

Typically you should use the unload event in the body (for example in jQuery, just add

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

      



The prototype has something like this, and for pure Javascript, I think it still depends on the browser you are using, but window.unload = function(evt){return false;}

might work.

Can't remember the correct syntax for it.

but I don't know if you can restrict this to only the back or if it will run for all uploads (like clicking on a link, closing the browser, etc.).

If you want to stop them because they might have unsaved data in the form, then that's okay. If you want them not to return for another reason, I think you should rethink why.

+4


a source


Typically, if using the back button can cause problems, you already have big problems.

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



  • Use POST for all requests that change data
  • Use nonces (unique identifiers) to prevent forms from being submitted twice
+1


a source


I am using noscript for this very reason. I insist on being in control of my browser, not the site I visit. I only allow scripting for specific sites. For any site that disables my back button, I do not allow it to run scripts.

0


a source







All Articles