Catch a refresh

How to catch a "website refresh" before it refreshes the site

There is not really a way this can be reliably be done across all browsers.

Some information in this context:

1 Like

It is OK if it only works on Chrome but where to put the javascript in my My delphi code

You could try to add an event handler to form.OnBeforeUnload

Just checking incase there is something built into WebCore now?

Since, as I already informed here, there is no universal solution for this, we haven't implemented something so far.

For others, this is working for me so far. If I find a browser that doesn't work, I will have to find a solution for it too, if I can. In my case, the webapp is only used internally by the company employees, so the browser choice is not that wide.

class procedure THtml.Prevent_Refresh;
begin
  asm
    document.addEventListener("keydown", function (event)
    {
      if (event.key === "F5" || (event.ctrlKey && event.key === "r"))
      {
        event.preventDefault();
      }
    });
  end;
end;

class procedure THtml.Warn_Tab_Close;
begin
  asm
    window.addEventListener("beforeunload", function (event)
    {
      event.preventDefault();
      event.returnValue = "";
    });
  end;
end;