Hi Guillermo,
Where are you attaching these listeners? I played around with this today for a bit, and although I didn't get it working properly, I avoided the problems that you were hitting. I started by creating the listener and saving it to the window:

I then added the listener in the Pre scripts of each coach. This is to ensure that if the form is re-entered, the listener is attached. Note that you want to add the listener in the diagram, not the page, otherwise you'll be listening to the wrong iFrame. To prevent the page completion from firing the event, you need to remove the listener in the Pre script of the end event:

Like I said, the above isn't perfect, and it seems to work better in Process Portal than Workplace, but I think it is the right idea. You need to carefully manage the listener and attach it to the iFrame of the CSHS diagram, not the page itself.
Hope this helps a bit,
Grant.
------------------------------
Grant Taylor
------------------------------
Original Message:
Sent: Thu June 19, 2025 03:19 AM
From: Guillermo Rodríguez Cuenca
Subject: IBM BAW capture window close event when closing the window directly from the browser
Hello,
We are currently working with IBM Business Automation Workflow (BAW), and we are trying to detect when a user closes the browser tab or window using JavaScript (e.g., via window.onbeforeunload
or window.onunload
).
The issue we're facing is that these events are also triggered during standard navigation within the BAW flow, such as moving from one Coach to another or accessing another screen in the same task. This makes it impossible to reliably distinguish between a real tab/browser close and a normal user action within the application.
We've considered workarounds using flags in sessionStorage
or custom button logic to detect internal navigation, but this approach is not ideal and can be difficult to maintain across the whole solution.
Is there any official or recommended way in IBM BAW to reliably detect when a user is closing the browser window or tab, as opposed to navigating within the BAW runtime? Our goal is to be able to capture this specific event in order to warn the user before leaving and to log this action for audit or troubleshooting purposes.
Any guidance or clarification would be greatly appreciated.
Best regards,
Guillermo
------------------------------
Guillermo Rodríguez Cuenca
------------------------------