OnBeforeUnload event in jquery
In this article I'm trying to explain how to work around onbeforeunload event in scripting functions. Recently I'm facing one issue while taking user logs against each page for each and every user. Log in time I take it from Page_Load event but log out time it's somewhat difficult to take it for me. Initially I did this while redirect time, but that is not suggestible. So, I found solution by searching in Google and the solution is Onbeforeunload event in jquery.
OnBeforeUnload event in jquery:
In this article I'm trying to explain how to work around onbeforeunload event in scripting functions. Recently I'm facing one issue while taking user logs against each page for each and every user. Log in time I take it from Page_Load event but log out time it's somewhat difficult to take it for me. Initially I did this while redirect time, but that is not suggestible. So, I found solution by searching in Google and the solution is Onbeforeunload event in jquery.Description:
Sometimes, we need to do some stuff while leaving the current page; this event will fire while leaving the page. So, we can perform the action whatever we want.Javascript :
<script type="text/javascript" language="javascript">
$(document).ready(function () {
window.onbeforeunload = function () {
$.ajax({
type: "POST",
url: "xxx.aspx/LoginDetails",
data: "{ 'LogId':'" + $("#<%=hdnLogId.ClientID %>").val() + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
response(data.d);
}
});
}
</script>Code Behind:
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static DataSet LoginDetails(string LogId)
{
//perform log out action on here.
}