Search code examples
cloudflare-apps

cannot react to close tab event with cloudfare app


I am using the app creator and trying to react to close tab window event using the code below. I then preview the app in a separate window, but when I close the tab I don't get the confirmation pop up. When I inject this code in the js console it works as expected. Doesn't cloudfare app support such functionality?

    window.onbeforeunload = function (e) {
// Your logic to prepare for 'Stay on this Page' goes here

    return "Please click 'Stay on this Page' and we will give you candy";
};

Solution

  • I tested this and was able to see the pop up when after clicking to close the tab. Are you certain that this assignment is happening? In the previewed window, what is the output of window.onbeforeunload?

    You also need to make sure to set the returnValueof e to something other than null e.g. :

      function sendAlert() {
        window.onbeforeunload = (e) => {
          const dialogText = 'Random Text';
          e.returnValue = dialogText;
          return dialogText;    }
      }