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

后端 未结 5 1210
面向向阳花
面向向阳花 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.

    0 讨论(0)
  • 2020-12-09 08:40

    There are two possible ways forward with this, both quite different.

    One way would be to have an event handler bound to onbeforeunload event so that you can detect when the user is browsing away from the current page. If my memory serves me correctly however, onbeforeunload is not consistent across browsers (I don't think Opera responds to it IIRC, but have no way to currently test). Of course, this solution fails if the user turns off JavaScript.

    The second and more robust way would be to implement the Post Redirect Get pattern which when used, prevents the data from being posted again when a user refreshes the page.

    0 讨论(0)
  • 2020-12-09 08:54

    All the answers are quite old, as of today 2020, according to HTML specification, you can do it this way.

    Important Note: Custom text is not supported in most of the browsers now. (it was supported in older browsers).

    https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

    window.addEventListener('beforeunload', function (e) {
      // Cancel the event
      e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
      // Chrome requires returnValue to be set
      e.returnValue = '';
    });
    
    0 讨论(0)
  • 2020-12-09 09:01

    There isn't a way to tie it to just the refresh action, but you may want to look into window.onbeforeunload. This will allow you to run a function that returns a string just before the page is unloaded. If this string is not empty, then a popup confirmation dialog, containing this string and some other boilerplate text provided from the browser.

    For example:

    window.onbeforeunload = function () {
        if (someConditionThatIndicatesIShouldConfirm) {
            return "If you reload this page, your previous action will be repeated";
        } else {
            //Don't return anything
        }
    }
    

    Also, if the current page was loaded via a POST operation, then the browser should already display a confirmation box when the user tries to refresh it. As a general rule, any action that changes the state of the data on the server should be done through a POST request, rather than a GET.

    0 讨论(0)
  • 2020-12-09 09:03

    You can do it like this:

    window.onbeforeunload = function() {
      return "Data will be lost if you leave the page, are you sure?";
    };
    

    This would show a prompt to the user allowing them to cancel. It's not refresh specific, but for your purposes (like editing a question on SO) that doesn't seem to matter, it's loss of info no matter where you're leaving to.

    0 讨论(0)
提交回复
热议问题