0

I am working on a website, where at a time one admin can login. I implemented this by saving value in database. Now the problem is when current admin forget to logout, the value in database do not change and another admin can't login again. Even this current admin can't login later on, cause the value in database is checked in condition. I have looked in window.onbeforeload and window.onunload functions, but it only trigger on page refresh etc, not detecting browser close tab/window. I want to detect closing browser tab and call function upon close, so I can change value in database using ajax.

Any help will be appreciated! Thanks

M Shahzad Khan
  • 935
  • 1
  • 9
  • 22
  • 1
    What if power is shutdown ? You need another solution. Maybe a timestamp to see if user is inactive. – DontVoteMeDown Mar 27 '14 at 14:26
  • How timestamp can be used for user inactive check? – M Shahzad Khan Mar 27 '14 at 14:28
  • Consider that each page updates that timestamp(on user's table on database) with the current time. When user will log on and the login system checks that the user is *active*, it has a second check that if the timestamp is older than a given time(e.g. 5 minutes), then perform the log on. – DontVoteMeDown Mar 27 '14 at 14:32

2 Answers2

0

The event you are looking for is onbeforeunload

Mannekenpix
  • 744
  • 7
  • 15
0

Use window.uneforeunload which is trigged when tab/window is closed.

Updated

 window.onbeforeunload  = function (){
  // update your database from here before page closed
 }

DEMO

Suman Bogati
  • 6,289
  • 1
  • 23
  • 34