如何检测与ASP.NET页面关闭事件 [英] how to detect the Page Close Event with ASP.NET

查看:970
本文介绍了如何检测与ASP.NET页面关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版和内容页面ASP.NET Web应用程序,从母版,当我点击菜单项来打开一个新的aspx页面。如果我想关闭新页面浏览器选项卡,我想说明一个弹出或者提醒他关闭浏览器选项卡用户的对话框。我不知道如何检测关闭browserTab按钮。
我用下面的代码在新的aspx页面:

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:

<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.

推荐答案

这完美的作品。

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') ;
    }   
}

这篇关于如何检测与ASP.NET页面关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆