Festy Posted February 12, 2008 Share Posted February 12, 2008 Hello folks, My question is same as the title of this thread. "How do I detect closure of a tab in firefox and new versions of IE? I need this is to know because I want to 'run some scripts/call a php function' when a tab is closed. Please don't suggest me 'onunload' because it doesn't work as when you refresh your browser, that event is triggered. Can anyone help? Quote Link to comment Share on other sites More sharing options...
awpti Posted February 12, 2008 Share Posted February 12, 2008 Wrong place to ask this. PHP can not help you with this need. This is purely client-side code and will require javascript. Quote Link to comment Share on other sites More sharing options...
KrisNz Posted February 12, 2008 Share Posted February 12, 2008 http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm Quote Link to comment Share on other sites More sharing options...
Festy Posted February 12, 2008 Author Share Posted February 12, 2008 http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm I appreciate your help but - Please don't suggest me 'onunload' because it doesn't work as when you refresh your browser, that event is triggered. Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 Can you not use the javascript in the header like the post above but make a call to a php script rather then giving the user a yes/no exit box when they close the tab? <script language="JavaScript"> window.onbeforeunload = confirmExit; function confirmExit() { <?php insert code here ?> } </script> I havent tested it but it should just process the php on any closure. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 12, 2008 Share Posted February 12, 2008 No it wouldn't. PHP is processed at page load. But if you put some AJAX in place of the PHP bit there, you could post to a PHP script. Quote Link to comment Share on other sites More sharing options...
Festy Posted February 12, 2008 Author Share Posted February 12, 2008 Can you not use the javascript in the header like the post above but make a call to a php script rather then giving the user a yes/no exit box when they close the tab? <script language="JavaScript"> window.onbeforeunload = confirmExit; function confirmExit() { <?php insert code here ?> } </script> I havent tested it but it should just process the php on any closure. As I said, even if that work, the onunload will get called even when you navigate to different page on same site or when you refresh the same page. So, if I call a logout function on 'onUnload', the user will be logged out as soon as he leaves the page or refresh it. (which I don't want) Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 12, 2008 Share Posted February 12, 2008 Not sure if it can be done then. Thought about sessions, but they are only destroyed when you close the whole browser I think. Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 Can I ask the purpose of this to see if there might be an alternative ? Quote Link to comment Share on other sites More sharing options...
nethnet Posted February 12, 2008 Share Posted February 12, 2008 PHP can't do this. It is completely server-side and will only be parsed when the page is loaded. No additional PHP will be executed when the tab is closed. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 12, 2008 Share Posted February 12, 2008 He already gave us that So, if I call a logout function on 'onUnload', the user will be logged out as soon as he leaves the page or refresh it. (which I don't want) Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 To cut a long topic short, I dont think there is any way to diff between a user navigating away from a page and a user closing his or her tab. The best your going to get is as per the other guys post about sessions but these will only close when the browser is closed. Quote Link to comment Share on other sites More sharing options...
Cognito Posted February 12, 2008 Share Posted February 12, 2008 Got a little intrested and looked a bit more into it Javascript Event Handlers might be able to help you http://dean.edwards.name/weblog/2005/10/add-event2/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.