Question
INFOSYS
IN
Last activity: 1 Jun 2016 13:36 EDT
When ever the browser closes i need to call a service explicitly
Hi,
I have a scenario where i need to call a service explicitly on browser close button. I want to know is there any OOTB activity or any API to handle this functionality??
Thanks,
Sarath Raju.
Message was edited by: Marissa Rogers - added category
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Pegasystems Inc.
IN
One of the scenario is described here where custom function is called on browser close:
https://pdn.pega.com/forums/user-interface/call-function-close-window-browser-x
You can try this approach.
Pegasystems Inc.
AU
Hello,
I don't think this is feasible in PRPC, you won't be able to register a browser close event. You can't configure any JavaScript to run correctly when closing a browser window or tab and have it call back to the server. This is actually by design and you can run scripts based on mouse position and get the code to run only when clicking "x" from tab or main browser window but Chrome and Firefox will actually block AJAX requests from client to server onbeforeunload and unload events.
Each HTTP request requires a response. There is no real send in HTTP. You will try a GET or POST and each returns a response. The browser window is closing so where will the response go? The application has to send you a response and it's going to. So really this should be a synchronous request. but on any type of unload event that is also a bad idea. What if the server is down? What if the client went out of WIFI range? The window won't close until it gets a response so now you have to configure a really small timeout. It just ends up being a really unreliable process that will cause extreme frustration.
The enclosed was tried by our SME with IE, outside PRPC:
<!DOCTYPE html>
<html>
<script>
window.addEventListener("beforeunload", CallAppCloseWindow, false);
Hello,
I don't think this is feasible in PRPC, you won't be able to register a browser close event. You can't configure any JavaScript to run correctly when closing a browser window or tab and have it call back to the server. This is actually by design and you can run scripts based on mouse position and get the code to run only when clicking "x" from tab or main browser window but Chrome and Firefox will actually block AJAX requests from client to server onbeforeunload and unload events.
Each HTTP request requires a response. There is no real send in HTTP. You will try a GET or POST and each returns a response. The browser window is closing so where will the response go? The application has to send you a response and it's going to. So really this should be a synchronous request. but on any type of unload event that is also a bad idea. What if the server is down? What if the client went out of WIFI range? The window won't close until it gets a response so now you have to configure a really small timeout. It just ends up being a really unreliable process that will cause extreme frustration.
The enclosed was tried by our SME with IE, outside PRPC:
<!DOCTYPE html>
<html>
<script>
window.addEventListener("beforeunload", CallAppCloseWindow, false);
function CallAppCloseWindow(e)
{
var evtobj=window.event? event : e;
var isWindowHardClose = false;
if (evtobj == e)
{
//Chrome/Firefox wont populate clientY when outside client window - no negatives maybe or beforeunload has no clientY? So really in Chrome and firefox i can't tell if the end user event was from outside of the client area and if the event is coming from closing using the "x". I have tried pageY etc ... just put a break point here....nothing to determine if the request should really be fired.
if (!evtobj.clientY) {
isWindowHardClose = true;
}
}
else {
//IE - will populate clientY with a negative value.
if (evtobj.clientY < 0){
isWindowHardClose = true;
}
}
if (isWindowHardClose) {
//this fires fine in IE but not in Chrome and Firefox - debugger shows "aborted" on the send.
var oXmlHttp = new XMLHttpRequest();
//this is a synchronous call .. not good without a handler and a timeout timer.
oXmlHttp.open("GET", "PROVIDE_URL_HERE", false);
oXmlHttp.send();
}
}
</script>
<body>
<h2>OnBeforeUnloadTesting</h2>
</body>
</html>
INFOSYS
IN
I need service side validation ...not in client side validation
INFOSYS
IN
here what i'm trying to asking is, similar to Requester level cache clearing when closing the browser there will be some service in backend at server side to clear the cache because cache cannot hold for long time for requester level....so we want that OOTB mechanism to clear that cache at server side......we want to implement similar approach for one of my enhancement.
Pegasystems Inc.
GB
Hi Gowri,
I agree with Basavara's analysis : there is no real reliable mechanism that guarantees you can trap a browser close event, and deliver a message to the server.
You could instead consider a 'watchdog' idea: where the server starts a 'count-down timer' : andthe client has to continually 'ping' the back-end to say "I'm still here"; which resets the server count-down each time it is received.
If the countdown reaches zero ; then the server would assume the client has disappeared (for whatever reason).
In fact: I *think* PRPC already has some code shipped with it to perform a 'keep-alive' similar what I'm describing above...(But I can't remember what it is called, or how it is configured at this point : anyone know what I'm trying to remember here ?)
If you are using a modern browser *and* a suitable App Server : you could look to using a 'Websocket' to hold open a (full-duplex) connection between the server and client: presumably (I haven't tried this) if you close your browser, the server will (quickly?) get notified that the client has disconnected ?
Cheers
John
Pegasystems Inc.
US
Using websockets sounds like a great idea - they're made for this sort of thing. As Basavaraj said, it's not possible to do this consistently by triggering off of actual browser close.
Can you post what you want the service to do once the browser closes? There might be an easier way to do it.