<script type="text/javascript">
window.onbeforeunload = function () {
myUnloadEvent();
alert('Calling some alert messages here');
return;
};
function myUnloadEvent() {
var pageUrl = '<%=ResolveUrl("~/Service/Customer.asmx")%>'
var parameters = "{}";
$.ajax({
type: "POST",
url: pageUrl + "/CustomerLogout",
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
}
function OnSuccessCall(response) {
alert('S');
}
function OnErrorCall(response) {
alert('E');
}
</script>
Above code may not be working on all browser..
So Second method will work on all browser.
---------------------second method for all browser--------------------
step 1:
make a webservice(Customer.asmx) which have the function name (CustomerLogout());
--------------------------
step:2
call the service by scriptmanager...
<asp:ScriptManager ID="ScriptManager" runat="server" >
<Services>
<asp:ServiceReference Path="~/service/Customer.asmx" />
</Services>
</asp:ScriptManager>
step:3
make the js file CloseBrowser.js
/** start Js Code
* This javascript file checks for the brower/browser tab action.
* It is based on the file menstioned by Daniel Melo.
* Reference: http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed
*/
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
Customer.CustomerLogout();
}
function wireUpEvents() {
/*
* For a list of events that triggers onbeforeunload on IE
* check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
*/
window.onbeforeunload = function () {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$('document').bind('keypress', function (e) {
if (e.keyCode == 116) {
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});
// end js code
------------------
Step 4:
call on pages
<script src="CloseBrowser.js" type="text/javascript"></script>
and also call
<script src="jquery.min-1.4.4.js" type="text/javascript"></script>
or any jquery Version
window.onbeforeunload = function () {
myUnloadEvent();
alert('Calling some alert messages here');
return;
};
function myUnloadEvent() {
var pageUrl = '<%=ResolveUrl("~/Service/Customer.asmx")%>'
var parameters = "{}";
$.ajax({
type: "POST",
url: pageUrl + "/CustomerLogout",
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
}
function OnSuccessCall(response) {
alert('S');
}
function OnErrorCall(response) {
alert('E');
}
</script>
Above code may not be working on all browser..
So Second method will work on all browser.
---------------------second method for all browser--------------------
step 1:
make a webservice(Customer.asmx) which have the function name (CustomerLogout());
--------------------------
step:2
call the service by scriptmanager...
<asp:ScriptManager ID="ScriptManager" runat="server" >
<Services>
<asp:ServiceReference Path="~/service/Customer.asmx" />
</Services>
</asp:ScriptManager>
step:3
make the js file CloseBrowser.js
/** start Js Code
* This javascript file checks for the brower/browser tab action.
* It is based on the file menstioned by Daniel Melo.
* Reference: http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed
*/
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
Customer.CustomerLogout();
}
function wireUpEvents() {
/*
* For a list of events that triggers onbeforeunload on IE
* check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
*/
window.onbeforeunload = function () {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$('document').bind('keypress', function (e) {
if (e.keyCode == 116) {
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});
// end js code
------------------
Step 4:
call on pages
<script src="CloseBrowser.js" type="text/javascript"></script>
and also call
<script src="jquery.min-1.4.4.js" type="text/javascript"></script>
or any jquery Version