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? Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/ 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464650 Share on other sites More sharing options...
KrisNz Posted February 12, 2008 Share Posted February 12, 2008 http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464671 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464714 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464716 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464727 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) Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464730 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464731 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 ? Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464734 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464736 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) Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464737 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. Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464739 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/ Link to comment https://forums.phpfreaks.com/topic/90639-how-to-detect-close-tab-in-firefox-and-ie7/#findComment-464743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.