Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to know that user leaving a page with asp.net?

Tags:

c#

asp.net

events

My question is a little bit tricky. at least I think. Maybe not, anyway. I want to know if there's any way to know if the user is leaving the page. Whatever if he clicks "Previous button", closing the window or ckicking on a link on my website. If my memory's still good, I think it's possible with JavaScript.

But in my case, I want to do some stuff (cleaning objects) in my codebehind.

like image 431
Simon Dugré Avatar asked Oct 19 '25 13:10

Simon Dugré


1 Answers

There really is no way to do it. No event is fired when the browser goes back.

You can do it with Javascript, but it is difficult at best.

See the question here.

This script will also work. It was found here

<script>

window.onbeforeunload = function (evt) {
var message = ‘Are you sure you want to leave?’;
if (typeof evt == ‘undefined’) {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}

</script> 
like image 135
David Basarab Avatar answered Oct 21 '25 03:10

David Basarab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!