25
Views
4
Comments
Clear Cookie On Browser or Tab Close
Question
Application Type
Reactive

Hi team,

We have 2 application that's shared between 2 sub-domains (eg. abc.outsystems.com and def.outsystems.com). To share some information across different screens, since they are in different subdomains, we are using cookies.

How to clean these cookies ONLY on browser/tab close?

Understand that we can use 'beforeunload' Javascript event. But this gets triggered even on page refresh and navigations. Also from different sources, it seems that beforeunload is not compatible with all browser or OS. Is there any other solution to cater for my scenario using cookie that works all across?

Appreciate the response here.

Thanks!

2025-12-22 13-50-43
Sherif El-Habibi
Champion

Hello @Angel Saxena,

To clear a cookie in OutSystems, you can simply use GetCookie to retrieve the existing cookie and then call SetCookie again with its value set to null. That part works correctly.

However, regarding detecting when the tab is closed, you are right. Both tab closing and page refreshing trigger the beforeunload JavaScript event. Based on everything I’ve found there is no direct or reliable way to detect only a tab close event. Browsers intentionally treat tab close, refresh, and navigation the same.

2021-09-06 15-09-53
Dorine Boudry
 
MVP

how about session cookies ? (see this explanation, don't give it a Expiration date)

2024-07-12 05-57-50
Gourav Shrivastava
Champion

Hello @Angel Saxena 

You cannot reliably detect tab close in modern browsers.  As @Dorine Boudry suggests that the safest solution is to use a session cookie (a cookie without an expiry). Session cookies are deleted automatically when the tab/window closes, but stay when you refresh or navigate.

Example:-

document.cookie = "MyData=ABC; path=/; domain=.outsystems.com; SameSite=None; Secure;";


2020-11-23 05-30-56
Angel Saxena

Hi all,

Thanks for all your responses.

We are using Session Cookie. But browsers require explicit settings to enable the cleanup of cookie when browser is closed. 

Since we are using cookies to check authentication status of user, hence we are a bit unsure if users don't have the cleanup setting turned ON.

Any suggestions around this or any other better solution would greatly help.

Thanks!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.