0
window.onbeforeunload = function() { logout();};
function Logout()
  {
     window.location.href = "logout URL";
   return false;
  }

My requirement is: when user clicks close button, user should be logged out. It is working fine in IE. But not working in Chrome.

Can any one please suggest.

  • 1
    You have a lowercase L for `Logout()` in your code - is that a typo, or in your code? – CodingIntrigue Aug 30 '13 at 06:46
  • possible duplicate of [window.onunload is not working properly in Chrome browser. Can any one help me?](http://stackoverflow.com/questions/7794301/window-onunload-is-not-working-properly-in-chrome-browser-can-any-one-help-me) – Sphvn Aug 30 '13 at 06:47

3 Answers3

4

Your function name is Logout() not logout(). Case Sensitivity

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • 1
    Sorry it is lower case only logout() – user2632735 Aug 30 '13 at 06:56
  • No. Iam writing this script in JSP page – user2632735 Aug 30 '13 at 07:08
  • @user2632735 you should update your post to correct this typo to not mislead people to answer on something that isn't the actual problem. –  Aug 30 '13 at 07:30
  • sorry for that. Any idea on resolving the issue. – user2632735 Aug 30 '13 at 09:18
  • @user2632735 Seriously, it is unconvenient way of logging user out. You can store the time when user logged in then start a loop in JS to send AJAX request to PHP and see which user is inactive for more than 20 seconds. If found anyone, set it as logged out. I cannot write full code for you but its a suggestion. – Muhammad Talha Akbar Aug 30 '13 at 09:25
  • @ Akbar. Thanks for that. But we have mechanism for inactive time. We have a req where user should be logged of if user closes the browser. User session should be terminated for that reason iam calling logout URL. – user2632735 Aug 30 '13 at 09:35
  • @user2632735 I have dealt with login systems, I applied the same idea as explained above. It worked nicely. – Muhammad Talha Akbar Aug 30 '13 at 10:46
1

Try,

window.onbeforeunload = logout;

function logout() {
  window.location.href = "logout url";
  return false;
}

UPDATE:

First is to check whether logout function is ever getting called by putting alerts in logout function or if you are using newer versions of chrome then use console.log('called').

You can try this alternative to check:

window.onbeforeunload = function() {
    logout();
    return null;
}

function logout() {
     window.location.href = "logout url";
}
sjain
  • 23,126
  • 28
  • 107
  • 185
  • 1
    Why not just `window.onbeforeunload = logout;` – Sphvn Aug 30 '13 at 06:49
  • Code optimizations are always great. Thanks for clarifying. Agreed! – sjain Aug 30 '13 at 06:50
  • what is console.log('called') which one i need to replace by called? – user2632735 Aug 30 '13 at 07:42
  • first try with alerts to check if `logout` is ever got called. – sjain Aug 30 '13 at 07:44
  • do you want me to show alert when we access close button? We are getting alert message when we try to close window. But we are not logging of the user. Is there any issue with window.location.href in chrome? – user2632735 Aug 30 '13 at 09:02
  • Well probably in modern browsers. You can try, `window.location.assign("logout url");` – sjain Aug 30 '13 at 09:11
  • Please try to put actual url in the href and also try the setTimeout method that should work on all browsers: `setTimeout(function(){window.location.href = "logout.html;"},500);` – sjain Aug 30 '13 at 09:22
0
logout() !== Logout()

Try the below one

window.onbeforeunload = logout;
function logout()
  {
   window.location.href = "logout URL";
   return false;
  }
Praveen
  • 55,303
  • 33
  • 133
  • 164