onunload or onbeforeunload

onunload or onbeforeunload

  

Good Day,

May I kindly ask what should be used between the two.

I can't make both work on my application.

My goal is, if the Window/Tab is closed, a record has to be deleted in the database.

We are using Java Stack 9.0.0.19.

Thank you and kind regards,

Christopher

Hi Christopher,

Maibe this post can help, even being very old.

How have you been trying to do it?

Cheers,

Eduardo Jauch.

Hi Jauch,


Same as posted.

But it was not mentioned if the method of the button should be Ajax Submit, but that's what I did.

Will the onunload event be triggered even if the 'x' icon of the Browser Tab or Main Browser itself used?

Thanks and kind regards,

Christopher

Hi Christopher,

Could you provide a minimum working module with your code to us to take a look?

Cheers, 

Eduardo Jauch

Those events are really tricky since developers abused them in the past to prevent windows/tabs from closing or trigger ads ...so what browsers allow during those events is somehow limited.

Do you really need to make your application depend on that behaviour? What about browser crashes, power failures or internet connectivity loss? Aren't you just trying to hide am applicational problem if the application requires catching browser close events?


Regards,

João Rosado

Hi Christopher,

Besides what João pointed, you could use the beforeunload to warn the user and let him decide if he wants to close or not (that is the default use for it).

In any case, you could tell a little bit more about your scenario so we could give some ideas on how to avoid this.

I made a small test and found that using the "click" method to click in a hidden link didn't worked... Maybe the click is not happening in time or the browser is blocking it.

But if you have the hidden link pointing to a screen action and use the snippet here (making the required changes), if the user closes the tab, it works.

I only tested this, and only on chrome.

"<script>
function myfunction(){
    OsAjax(arguments[0] || window.event,'DublinTheme_wt12_block_wtMainContent_wt6_wtHiddenLink','DublinTheme_wt12$block$wtMainContent$wt6$wtHiddenLink','','__OSVSTATE,','');
    return false;
}
window.addEventListener('beforeunload', myfunction);
;</script>"

But I think it is really bad practice to do this (not to mention you need to define the id and the other parameter dynamically, probably).

That's one thing that browsers try really hard to stop you from doing.

On this thread I discussed a few options, you might take a look into that.

Hi Guys,

Thank you all for the suggestions.

The requirement given to me by my Lead is to prevent the User to use multiple Browser Tabs for a single application.

The user is allowed to User different applications, but only 1 browser tab each.

Is there a javascript that redirects a browser tab if the URL used is already loaded in the browser?

Thank you and kind regards,

Christopher

Hello Christopher.

That is not the way the web works. You cannot control how many tabs the user has open, and cannot redirect from one tab to the other. Besides, today you have multi-device experiences, and you certainly cannot prohibit your users from opening your site in desktop and also in mobile at the same time.

Is there any specific problem with your app that prevents your users from using multiple tabs?