如何检测asp.net中的浏览器和窗口关闭我正在使用window.onbeforeunload



嗨,伙计们,我正在使用onbeforeunload方法。但当用户通过点击网站中的任何链接/刷新不需要的页面来导航到任何其他页面时,它就会触发。我只需要在用户关闭浏览器时找到,而不是导航或刷新页面等。不是吗。谢谢

<script type="text/javascript">
window.onbeforeunload = function (e) {
var e = e || window.event;
if (e) e.returnValue = 'Browser is being closed, is it okay?';//for IE & Firefox
return 'Browser is being closed, is it okay?';// for Safari and Chrome
};
</script>

这对于服务器来说是不可能检测到的,因为html被发送到客户端,并且没有任何活动套接字。解决方法是与访问您网页的用户建立一个套接字,当这个套接字关闭时,您可以假设用户已经关闭了页面。但即便如此,你也无法检测到用户正在关闭浏览器窗口。

最新更新