0

How can i control when somebody press exit button on firefox, chrome, IE, safari or control when the presh the fan close button and then delete them from the database, actually i know that. The only problem i got is how to catch that button press.

I got some code but it doesn't seem to work. the code i got looks like this

on my default.aspx.cs

public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler


string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "LogOutUser", "");
        string logOutUserCallBackScript = "function LogOutUserCallBack(arg, context) { " + callBackReference + "; }";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, true);

void System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

     _callBackStatus = "failed";

     // log out the user by deleting from the LoggedInUser table
     DataClassesDataContext db = new DataClassesDataContext();

     var loggedInUser = (from l in db.BrugerOnlines
                         where l.BrugerId == Convert.ToInt32(Session["ChatUserID"])
                         && l.RumId == lb
                         select l).SingleOrDefault();

     db.BrugerOnlines.DeleteOnSubmit(loggedInUser);
     db.SubmitChanges();

     var besvarer = (from p in db.Rums where p.FK_Bruger == Session["SupportAdd"].ToString() select p).SingleOrDefault();
     db.Rums.DeleteOnSubmit(besvarer);
     db.SubmitChanges();
     // insert a message that this user has logged out
     this.sendbesked("Bruger har lukket siden " + DateTime.Now.ToString());

     _callBackStatus = "success";
 }
string  System.Web.UI.ICallbackEventHandler.GetCallbackResult()
 {
     return _callBackStatus;
 }

and on my default.aspx it looks like this.

in the javascript on the top of the page it looks like this

function LogOutUser(result, context) {
        // don't do anything here
    }

    function LogMeOut() {
        LogOutUserCallBack();
    }

<div onload="SetScrollPosition()" onunload="LogMeOut()">
</div>

and the library i use is http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js and i have no idea with this code is a part of it

 $(function () {
    var timeout = 60000;
    $(document).bind("idle.idleTimer", function () {
        // function you want to fire when the user goes idle
        $.timeoutDialog({ timeout: 1, countdown: 60, logout_redirect_url: 'http://www.aspdotnet-suresh.com', restart_on_yes: true });
    });
    $(document).bind("active.idleTimer", function () {
        // function you want to fire when the user becomes active again
    });
    $.idleTimer(timeout);
});

3 Answers3

0

Call Your Method on onBeforeUnload of Page's Body

F11
  • 3,703
  • 12
  • 49
  • 83
0

If all you want is to do some action when the user closes their browser.

I don't see any reason to catch button event.

pressing the X button will lead to Session_End

just handle global.aspx Session_End

like in this example:

How to handle session end in global.asax?

NOTE: this won't happen the same instant when user quits but when seesion timeouts

Community
  • 1
  • 1
Nahum
  • 6,959
  • 12
  • 48
  • 69
  • But with i use that, then it wait's on the session to end right? because i made session timeout to be around 1-2 hours. – user1880497 Dec 24 '12 at 13:30
  • The Timeout property specifies the time-out period assigned to the Session object for the application, in minutes. If the user does not refresh or request a page within the time-out period, the session ends. I think thats kinda along time. why don;t you make it 10 minutes and solve your problem? – Nahum Dec 24 '12 at 13:39
  • Because i'm making a chat-application and i dont wont a session timeout on them. they can stay as long as they wishes. Only way i wishes to throw them out is when they use the close button. – user1880497 Dec 24 '12 at 13:44
  • so just do the cleanup once the seesion ends, what does it matter? – Nahum Dec 24 '12 at 13:47
0
  1. The jQuery idleTimer plugin that you are using is for detecting idle time for the page & based on timeout you are redirecting user.

  2. You can use: jQuery unload Event:

The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52