简体   繁体   中英

How can i control when somebody press exit button

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);
});

在页面主体的onBeforeUnload上调用您的方法

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM