OnClose Browser


Hello to the Outsystems Community!

Here's my doubt... is there anyway there i can trigger a confirmation message on close of the window?! Example... if i have a enter/quit or login/logout option integrated on my page and the user didn't quit and close the browser i would like to inform him that he should make logout before close. Is that possible?


Unfortunately seems there is no simple way around this.

You have the OnUnload and OnBeforeUnload javascript events. The problem with those, is they are also triggered whenever you submit a form or navigate to another page.
The easiest way would be to open your application in a secondary window, opened through javascript and then use the window.onclose event. The obvious limitation is you'd have to have two windows. One with the application, and the other to detect and handle the event.

If I really had to do this, I'd probably use jQuery with a control variable to exclude unnecessary messages.
Set a control variable myVar,
  • var myVar;
use jQuery to set the variable in each link/button
  • $('a').click(function() { myVar= true; });
  • $('form').bind('submit', function() { myVar= true; });
then validate it's value on the onbeforeunload event to avoid alerts on navigate/submit.
  • $(window).bind("beforeunload", function() { return myVar || confirm("Are you sure you want to leave?");});
I would try to avoid doing that confirmation message tho, especially if you're not confortable with using jQuery.
That is really a problem. The "2 window solution" is not the best solution because is not "correct" to do it. Eheh. But i would really need a solution and if that is the only solution probably i'll have to use it. Thanks for the reply. If there are any other suggestions i'll thank you community!
Hi Diogo,

I would sugest you to stay away from doing those kinds of stuff.
Unless its really needed, for example pages to write/edit Documents ..where the user can actually lose content.
Otherwise is more an annoyance than anything else.

Also one very common mistake on pages development is when developers start using Session to know what page the user is / or what he is doing.
Please ..everyone needs to learn this users in real life use multiple tabs or even multiple browsers! I've seen so many errors caused by this.

What is really the deal about forcing a user to Logout? Session by default lasts 20min (can can be configured), eventually they lose it and have to login again.
Forcing users to logout to free some "state" is a bad practice in my opinion.

João Rosado
Yeah, the two window solution isn't really anything you'd want to use. I just mentioned it to help you get into context. :)
As João said, you should really stay away from this kind of stuff whenever you can.

If you REALLY have to do it, I believe the jQuery solution is your only usable option.
Although you can do it with plain Javascript, doing so would force you to include javascript manually in every link and button of every page. (And that's just messy)