asp.net to determine when the user leaves and how to leave _ Practical skills
Source: Internet
Author: User
Site user statistics is every site than there is a function, but very few people realize the absolute accuracy of online user statistics or is relatively accurate, based on the principle of the Web, the server will not take the initiative to know the user by shutting down the browser to leave the site, this time can only rely on server-side variables to expire , but the user's browser can tell when they're off, and the code is 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 to close the browser or refresh or navigate to other pages!
---------------------------------------------------------------------------------------------------
In the afternoon, the code test, the main use of the above JS function, combined with Ajax technology, because of the knowledge of Ajax technology is not very thorough, before and after tried three times.
Now will try to share with you the results, we also give a little more advice
First I tried to access application in Ajax methods, trying to use application variables to record online numbers. Try code:
[Ajaxpro.ajaxmethod]
public int Logoff ()
{
application["Onlinecount"]=convert.toint32 (application["Onlinecount"])-1;
Return Convert.ToInt32 (application["Onlinecount"]);
}
Try to fail! It is estimated that Ajax cannot access the application's static and application variables. The first time use Ajaxpro do not know how to set access to the session, previously used is Ajax.dll, set to read and write session, the results may be changed!
The second I tried to record online numbers with a text file, still failed!
Third, the use of the database! Try to succeed.
The 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. Open ();
Return Convert.ToInt32 (cmd. ExecuteScalar ());
}
catch (Exception ex)
{
EventLog.WriteEntry (ex. Source,ex. message);
}
Finally
{
Conn. Close ();
}
return 0;
}
Front Page Indent
1 <script language= "JavaScript" >
2<!--
3 function Leave ()
4 {
5 alert (Window.event.clientX);
6 if (window.event.clientx<0)
7 {
8 WebApplication3.WebForm1.Logoff (). value;
9}
10}
11//-->
</script>
</HEAD>
<body onunload= "Leave ()" >
In this case, in the safety and timeliness of the requirements of the more stringent conditions, a page set as the main page, if the page is closed, indicating that the user has exited. The method is available to the e-mail system.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service