PDA

View Full Version : Browser closing event.



tullio0106
12 Jul 2010, 9:09 AM
I need to avoid the user closes the browser during an application or, bettere, I want to intercept the close action, do something and the close the browser.
I didn't find the way.
Unload event is fired every time a page si unloaded but I'm not able to see if the close button was pushed or the page changes for every other reason.
Could someone suggest a solution
Tks
Tullio

Condor
12 Jul 2010, 11:46 AM
You can't see the difference between a window close or navigating to a different url.

tullio0106
12 Jul 2010, 11:35 PM
I've a non stateless application and I need to stop it (releasing resources) when the user closes the browser but not when navigate in a different page of my application.
Any alternative or suggestion ?
Tks
Tullio

Animal
13 Jul 2010, 12:48 AM
I've a non stateless application and I need to stop it (releasing resources) when the user closes the browser but not when navigate in a different page of my application.
Any alternative or suggestion ?
Tks
Tullio

There's no 100% way. You cannot expect to control the browser.

You can hook the beforeunload event and fire off an Ajax request to tell the server to clean and destroy the session. That's your best shot.

Aside from that, the server session should clean up on timeout anyway.

tullio0106
13 Jul 2010, 1:33 AM
I tryed but the problem is : when beforeunload starts I don't know if next page will be a page of my applicantion (and then I should not release resources) or another page (or close).
Tks
Tullio

Condor
13 Jul 2010, 1:43 AM
What method does your application use to nativate between pages: form post+redirect or anchors?

ps. A true Web2.0 application doesn't use multple pages.

tullio0106
13 Jul 2010, 1:46 AM
Form Post.
I know about 2.0 but it's not a so new application.
Tks

Animal
13 Jul 2010, 1:49 AM
Intercept click events on all <a> elements and see whether they are leaving your app. Many ways to do that. Probably one document level listener and delegate: 'a' might be the best way.

Condor
13 Jul 2010, 2:13 AM
There is another way.

Make the main html page a frameset with one frame and put your application in the frame. Attach the beforeunload event handler to the frameset instead of to the frame.

When configured this way the beforeunload will only fire if you close the browser or nativate to a new url, but not for POST/redirect navigation.

Animal
13 Jul 2010, 2:17 AM
Euw! That's what our legacy web app used to do.

tullio0106
15 Jul 2010, 1:29 AM
We tested the frameset solution.
Unfortunately it seems to work only in part.
We still have problems with browser back button and history.
Any suggestion ?
Tks
Tullio

Condor
15 Jul 2010, 2:56 AM
Are you using Ext.History? In that case you might want to change all the 'top.location' references in Ext.History to 'location'.

tullio0106
15 Jul 2010, 3:08 AM
No, I'm speacking about browser history (recent pages) and back.
Tks
Tullio

Condor
15 Jul 2010, 3:53 AM
Are you sure your are using the frameset correctly? Navigating to a different page within your site should only reload the current frame and not replace the entire frameset.

tullio0106
15 Jul 2010, 5:15 AM
Using the browser back button or the reload the whole page is loaded and then the event beforeunload is fired.
Is there any way to avoid yhis ?
Tks
Tullio