PxPlus User Forum

Twitter Twitter Twitter

Author Topic: closing browser  (Read 1372 times)

Danilo David

  • Member
  • **
  • Posts: 18
    • View Profile
closing browser
« on: June 05, 2019, 11:20:35 PM »
hello,

is there any way to capture the closing of the browser in the webserver?
because i want to execute pxplus code after the user click the X to close the browser.

thank you
« Last Edit: June 05, 2019, 11:22:31 PM by Danilo David »

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: closing browser
« Reply #1 on: June 06, 2019, 11:04:10 AM »
Generally there is no guaranteed way to trap the closure of a web browser and certainly no way for PxPlus to handle or even be notified about it without you putting some code (JavaScript) in your web page to handle the issue.

Most browsers support an 'onbeforeunload' event where you can display a warning message if that is what you want.
You can also try trapping the 'onunload' event and have it send a Ajax style message to the server -- however at this point you cannot prevent the closure from happening.

Regardless of what you do you need to consider that the browser being closed may be due to circumstances beyond the control of the user or browser.  Power failure, reboot, forced close, loss of connection, etc.. are all reasons that your browser connection may be terminated.

What I would suggest is you create some form of background polling between the browser and PxPlus where you can consider no polls for a period of time as a disconnect.  This s what iNomads does in order to help assure the browser is still connected as well as setting an 'onbeforeunload' event handler.

As a side note, if you are looking to trap the browser closure so that you can 'save' information the user has entered you might consider using 'localStorage'.  You could create some JavaScript to preserve user input, form information and state in Local storage. You could re-use this later when the browser was restarted and the user re-connected to the site.  Perhaps by attaching to the onsubmit event you could save the data being submitted and target URL, then on reconnect re-submit the data.  Good web logic should already be capable of handling re-submissions so you likely would have little to change on the host and merely have to create some JavaScript to attach to the <form>.

Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

Danilo David

  • Member
  • **
  • Posts: 18
    • View Profile
Re: closing browser
« Reply #2 on: June 13, 2019, 02:27:36 AM »
mike,

the onbeforeunload event cannot be use to call pxplus code and onunload event is not working to all browser, it is only working good in internet explorer.

what I want to do is to call and execute pxplus code after they did close the browser.

thank you mike,

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: closing browser
« Reply #3 on: June 13, 2019, 09:38:48 AM »
As I mentioned, the onbeforeunload can be used to display a warning message -- and as per WWW standards that is all it can be used for.  In actual fact the original definition of this event was to allow you to display a message of your choosing to the end user, however current Chrome and FireFox (I believe) don't actually use the message you pass back, instead they display generic messages in the language of the user so you don't have to worry about providing messages in all possible languages.

The "onunload" is the only option you have and that can only send an asynchronous Ajax request that may or may not get received depending on timings.

Your safest bet is to have your web pages send periodic messages to the host to indicate they are still active and a background process on the server check for instances where these message are no longer being received to do whatever cleanup is required.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com