Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - detect browser stop button click

I have a form that disables submit button, when it is clicked. But what if the user clicks browser "Stop" button. Then he will not be able to resubmit the form.

Is there any way to handle such cases, possibly detecting Stop button press?

like image 810
Evgenyt Avatar asked May 29 '26 15:05

Evgenyt


1 Answers

What is the reason for disabling the submit button?

You are trying to avoid double-clicks? -> you can disable the submit button for only a brief period of time, re-enabling it again on a timeout.

You are trying to avoid impatient reload-clicking? -> the same, but with a longer inactivity period.

You are trying to stop a form being submitted twice causing duplicate actions to occur? -> you can't fight this just with button disabling, as going back/forward will cause the page to be reloaded, likely keeping old form content but not the disabledness state, unless short-circuited by bfcache. In this case you must create a one-use token or new item ID that cannot be used more than once, and put it in a hidden field in the form. The server can check for it and disallow duplicates.

possibly detecting Stop button press?

Avoid onstop, it's not really reliable. Apart from browser support issues, it can't catch all possible combinations of navigation and stop/reload/etc. You'll never know how far the server script got, whether it performed an action.

like image 107
bobince Avatar answered Jun 01 '26 05:06

bobince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!