Detect Browser Close on Asp.Net

Detect Browser Close on Asp.net

<script type="text/javascript">
var closing = true;
$(function () {
$("a,input[type=submit]").click(function () { closing = false; });
$(window).unload(function () {
if (closing) {
jQuery.ajax({ url: "http://localhost:49591/Account/LogOff", async: false });
}
});
});
</script>

Call the logout.aspx when window closes using javascript + jquery. Do whatever you want to do in the logout.aspx page load event.

The above snippet will have to be added in your master page's html.

Detect Close Browser in ASP.NET

Yes, you should use something like this:

<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You are about to exit the system before freezing your declaration! If you leave now and never return to freeze your declaration; then they will not go into effect and you may lose tax deduction, Are you sure you want to leave now?";
}
$(function() {
$("a").click(function() {
window.onbeforeunload = null;
});
$("input").click(function() {
window.onbeforeunload = null;
});
});
</script>

So this message is not showing everytime you refresh the page

Link here to the source

Is there a way to detect if browser has been closed in a asp.net web mvc 5.0 project?

You can detect the tab/browser being closed from javascript using the beforeunload event. Credited Source

window.addEventListener('beforeunload', function (e) {
// User is closing the tab/browser
// You can cancel this event by using e.preventDefault();
// and for older browsers by using e.returnValue = '';
});

From there you can send a message back to your application using AJAX to handle the temp data removal.

Below using JQuery

$.ajax({
method: "POST",
url: "AbandonSession",
data: { someVar: "some val" }
})
.done(function( msg ) {
alert( "Session abandoned: " + msg );
});

detecting closing tab or browser script not working

You should use onbeforeunload.

An event that fires when a window is about to unload its resources. The document is still visible and the event is still cancelable.

window.onbeforeunload = function (e) {
return 'Dialog text here.';
};

Docs: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload

EDIT

Use on instead of bind. You are using bind to attach events on elements.

$(window).on('beforeunload', function(e) {
return "If you close the window, your application with still be running. If you wish to stop the application, click 'cancel' and "
});

EDIT-2

The submit button will submit the form and will refresh the page unless you have prevented this action. If you want to prevent this from happening you should either preventDefault submit of the form or add a check on the unload event.

var submitting = false;
$('form').submit(function (e) {
submitting = true;
...

});

$(window).bind('beforeunload', function (e) {
if (!submitting) {
return "If you close the window, your application with still be running. If you wish to stop the application, click 'cancel' and "
}
return false; // Prevent the alert box
});

How to detect browser close at server side in asp.net?

client side script:

< body onbeforeunload="window.open('http://www.website.com/browserclosed.aspx','mywindow','width=1,height=1');">

server side script (browserclosed.aspx):

// page_load

int userId = Convert.ToInt32(request.session("userId"));
ReportBrowserClosed(userId);
// Do what you want in ReportBrowserclosed() method


Related Topics



Leave a reply



Submit