您通常会执行以下操作:
代码语言:javascript复制protected void btnClose_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
}但是,请记住,不同的场景中会发生不同的事情。火狐不会让你关闭一个不是你打开的窗口(用window.open()打开)。
IE7将提示用户“此页面正在尝试关闭(是|否)”对话框。
在任何情况下,你都应该准备好处理窗口并不总是关闭的问题!
上述两个问题的一个修复方法是使用:
代码语言:javascript复制protected void btnClose_Click(object sender, EventArgs e) {
ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.open('close.html', '_self', null);", true);
}并创建一个close.html:
代码语言:javascript复制
var redirectTimerId = 0;
function closeWindow()
{
window.opener = top;
redirectTimerId = window.setTimeout('redirect()', 2000);
window.close();
}
function stopRedirect()
{
window.clearTimeout(redirectTimerId);
}
function redirect()
{
window.location = 'default.aspx';
}
Please Wait...
请注意,如果窗口在2秒后由于某种原因没有关闭,close.html将重定向到default.aspx。