C# 如何使用 ASP.NET 检测页面关闭事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16584663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect the Page Close Event with ASP.NET
提问by H_79
I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a MenuItemto open a new aspx Page. if I want to close the new page browser tab, I want to show a popup or a dialog that alert the user that he is closing the browser tab. I dont know how to detect the close browserTab button.
I used the following code in the new aspx page:
我有一个带有 MasterPage 和内容页面的 ASP.NET Web 应用程序,当我单击 aMenuItem打开一个新的 aspx 页面时,来自 MasterPage 。如果我想关闭新的页面浏览器选项卡,我想显示一个弹出窗口或对话框,提醒用户他正在关闭浏览器选项卡。我不知道如何检测关闭 browserTab 按钮。我在新的 aspx 页面中使用了以下代码:
<script type="text/javascript">
function CloseWindow() {
alert('closing');
window.close();
}
</script>
new page code behind:
后面的新页面代码:
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}
Thanx in Advantage.
感谢在优势。
回答by Davi Ruiz
You can handle this with javascript in your page.
First create a function:
您可以在页面中使用 javascript 处理此问题。
首先创建一个函数:
function closeMessage() {
return "Are you sure you want to leave this page";
}
After that assign this method:
之后分配此方法:
window.onbeforeunload = closeMessage;
Please let me know if was helpfull
如果有帮助,请告诉我
回答by Potheek
You need to catch the event for closing the browser and use it to do whatever you wanna do. Here is the code that worked for me.
您需要捕获关闭浏览器的事件并使用它来做任何您想做的事情。这是对我有用的代码。
<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>
Hope this helps..
希望这可以帮助..
回答by Dinesh Haraveer
If I get you correctly, you want to know when a tab/window is effectively closed. Well, afaik your only way in Javascript to detect that kind of stuff are onunload & onbeforeunload events. Those events are also fired when you leave a site over a link or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close in Javascript. Correct me if I'm wrong here.you can go through browser-tab-close-detection-using-javascript-or-any-other-language
如果我理解正确,您想知道标签/窗口何时有效关闭。好吧,你在 Javascript 中检测那种东西的唯一方法是 onunload 和 onbeforeunload 事件。当您通过链接或浏览器后退按钮离开站点时,也会触发这些事件。所以这是我能给出的最好答案,我认为您无法在 Javascript 中本地检测到纯关闭。如果我在这里错了,请纠正我。你可以通过浏览器标签关闭检测使用javascript-或任何其他语言
回答by Aby
This works perfect.
这工作完美。
javascript detect browser close tab/close browser
<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">
var myclose = false;
function ConfirmClose()
{
if (event.clientY < 0)
{
event.returnValue = 'You have closed the browser. Do you want to logout from your application?';
setTimeout('myclose=false',10);
myclose=true;
}
}
function HandleOnClose()
{
if (myclose==true)
{
//the url of your logout page which invalidate session on logout
location.replace('/contextpath/j_spring_security_logout') ;
}
}
回答by Jelle
I had a lot of problems with this myself and the only thing that works is: "window.onbeforeunload" event from javascripts but the problem is this gets executed a lot more than when you'd expect. So basicly if your working with a master page etc I don't think it can be done.
我自己遇到了很多问题,唯一有效的是:来自javascripts的“window.onbeforeunload”事件,但问题是执行的次数比您预期的要多得多。所以基本上如果你使用母版页等,我认为它无法完成。
I think it's best you try to approach this differently because else your closing message will show up to often.
我认为你最好尝试以不同的方式处理这个问题,否则你的结束信息会经常出现。

