ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Callback function on browser close

July 03, 2012 7:30am

Subscribe [1]
  • #1 / Jul 03, 2012 7:30am

    Hallas

    2 posts

    Hi to all,

    I have a request from client to catch browser closing and log in DB table user id and session lenght.

    My CI version is 2.1.1 that has in config sess_expire_on_close, but in system/libraries/Session.php variable sess_expire_on_close is just mentioned as parameter and I can’t find any further use in file.

    Can someone guide me how to catch browser closing and bind callback to it ?

  • #2 / Jul 03, 2012 4:41pm

    skunkbad

    1326 posts

    Hi to all,

    I have a request from client to catch browser closing and log in DB table user id and session lenght.

    My CI version is 2.1.1 that has in config sess_expire_on_close, but in system/libraries/Session.php variable sess_expire_on_close is just mentioned as parameter and I can’t find any further use in file.

    Can someone guide me how to catch browser closing and bind callback to it ?

    Not possible. While you can warn a person that they are navigating away from the page and may lose what they were working on, there’s no way to do what your client is asking.

    You might search for onbeforeunload, which is a javascript method. This is as close as you can get to an event for browser close, but it’s going to give the site visitor a confirmation, and they can click yes or cancel.

    A solution might be to implement some javascript that pings the server with the user’s ID every 30 or 60 seconds, however, this might be expensive if you have a lot of users.

  • #3 / Jul 03, 2012 5:06pm

    PhilTem

    872 posts

    In your terms it would make a difference, if the user just browsed away from your page e.g. to go to Google.com or if he closed the browser window.

    However, you cannot detect the second one especially not in the case when the user is no longer on your side. What you can however do is to make your app fully AJAX based which allows you to use techniques used by GoogleMail, DokuWiki and FacebookMessage which will alert the user that he is moving away from a page (you know the prompt asking you whether want to stay or leave the current page).

    That’s for my knowledge the only possibility to do what you want on browser close.

    But, I just had another idea: If the page views aren’t for long times (i.e. longer than e.g. 5 seconds) you could just update the session-lifetime each time a new page is requested. This way you will actually always loose the last page or the time the user spent on the last page viewed respectively, but you can get an estimate of how long every user was browsing a page.

  • #4 / Jul 03, 2012 5:07pm

    WanWizard

    4475 posts

    Also know that things like reload, Ctrl+R, F5 etc will trigger the onbeforeunload event, while the user isn’t actually leaving the page. So making that reliable might prove to be a challenge.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases