How to pop up an alert box when the browser's refresh button is clicked?

后端 未结 5 1224
面向向阳花
面向向阳花 2020-12-09 08:16

If the user refreshes the page in question it will add another record to the database, so I want to warn the user through an alert box if they really want to refresh the pag

5条回答
  •  忘掉有多难
    2020-12-09 08:39

    This is not possible. The best you can do is use the onbeforeunload event but that will fire on any event leaving the current page. It is not possible to target the refresh button specifically.

    See e.g. this question on onbeforeunload

    It might be better though to build a duplicate check into your database. That would catch accidental submissions using the "back" button as well.

    An alternative would be using a random one-time token that gets built into the form. If two operations are attempted using the same token, you would stop it.

提交回复
热议问题