I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage when I click a MenuItem
to 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:
当我单击MenuItem打开一个新的aspx页面时,我有一个带有MasterPage和内容页面的ASP.NET Web应用程序。如果我想关闭新的页面浏览器选项卡,我想显示一个弹出窗口或一个对话框,提醒用户他正在关闭浏览器选项卡。我不知道如何检测关闭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.
Thanx在优势。
5 个解决方案
#1
3
This works perfect.
这很完美。
javascript detect browser close tab/close browser
javascript检测浏览器关闭选项卡/关闭浏览器
<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') ;
}
}
#2
1
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.
我认为你尝试以不同的方式处理这种情况是最好的,因为否则你的结束信息会经常出现。
#3
0
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
如果有帮助请告诉我
#4
0
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..
希望这可以帮助..
#5
0
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
如果我找到你,你想知道什么时候选项卡/窗口有效关闭。好吧,afaik你在Javascript中唯一的方法来检测那种东西是onunload和onbeforeunload事件。当您通过链接或浏览器返回按钮离开网站时,也会触发这些事件。所以这是我能给出的最好的答案,我不认为你可以在Javascript中原生地检测到纯粹的关闭。如果我在这里错了,请纠正我。你可以通过浏览器选项卡关闭检测 - 使用-javascript或任何其他语言
#1
3
This works perfect.
这很完美。
javascript detect browser close tab/close browser
javascript检测浏览器关闭选项卡/关闭浏览器
<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') ;
}
}
#2
1
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.
我认为你尝试以不同的方式处理这种情况是最好的,因为否则你的结束信息会经常出现。
#3
0
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
如果有帮助请告诉我
#4
0
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..
希望这可以帮助..
#5
0
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
如果我找到你,你想知道什么时候选项卡/窗口有效关闭。好吧,afaik你在Javascript中唯一的方法来检测那种东西是onunload和onbeforeunload事件。当您通过链接或浏览器返回按钮离开网站时,也会触发这些事件。所以这是我能给出的最好的答案,我不认为你可以在Javascript中原生地检测到纯粹的关闭。如果我在这里错了,请纠正我。你可以通过浏览器选项卡关闭检测 - 使用-javascript或任何其他语言