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.
}


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: