Judging when the user leaves, in what way to leave

xiaoxiao2021-04-04  246

Site user statistics is that every site is better than having functions, but few people have realized online user statistics absolutely precise or relatively accurate, based on web-based principles, the server will not take the initiative to know the user by closing the browser leaves the site, this time only Can rely on server-side variable session to determine if the user leaves, but the user browser can know when he is closed, the code is also very simple.

as follows:

Function body_onunload ()

{

IF (Window.Event.Clientx <0)

{

Alert ('the broswer is closing ...');

}

Else

{

Alert ('The User is Refreshing or Navigating Away ...);

}

The above code will determine whether the user is closing the browser or refreshing or navigating to other pages! -------------------------------------------------- -------------------------------------------------in the afternoon When I tested with the code, it was mainly to use the above JS function, combined with Ajax technology, because it is not very thorough for Ajax technology, and I tried three times before and after. Now, the results will be shared with you. Everyone gives some opinions. First, I tried to access the Application in the Ajax method, trying to use the Application variable to record the number of online people. Try code: [ajaxpro.ajaxmethod] public int logoff () {Application ["onlinecount"] = convert.toint32 (Application ["onlinecount"]) - 1; return convert.Toint32 (Application ["onlinecount"]);} Try failure! It is estimated that AJAX cannot access static variables and Application variables. The first time I use AjaxPro I don't know how to set up the session. It used to use Ajax.dll, set to read and write Session, and try the results possible to change! The second kind I tried to record the number of online people with text files and still failed! In the third, use a database! Try success. Code is as follows: [AjaxPro.AjaxMethod] public int Logoff () {SqlConnection conn = new SqlConnection ( "server = .; database = test; uid = sa; pwd = 123"); SqlCommand cmd = new SqlCommand ( "update OnlineCount set num = Num-1 ", conn); try {conn.open (); cmd.executenonquery ();} catch (exception ex) {eventlog.writeentry (ex. Source, ex.Message);} finally {conn.close );} Cmd = new sqlcommand ("SELECT TOP 1 Num from OnlineCount", CONN); Try {Conn (); Return Convert.Toint 32 (cmd.executescalar ());} catch (exception ex) {EventLog.WriteEntry (ex. Source, EX.MESSAGE);} finally {conn.close ();} return 0;} front page