如果用户确认关闭当前窗口,则在当前窗口关闭时打开新的浏览器窗口

时间:2012-07-28 15:49:51

标签: javascript jquery

我有一个场景,如果用户试图关闭当前的浏览器窗口,那么他应该显示一个确认框。如果他确认关闭窗口,则该窗口应该关闭并用不同的URL打开新窗口。

我尝试使用以下代码,通过该代码我可以显示确认框,但如果用户确认关闭当前信息,我无法弄清楚如何打开新窗口。

var preventUnloadPrompt;            
$('a').live('click', function () { preventUnloadPrompt = true; });
$('form').live('submit', function () { preventUnloadPrompt = true; });
$(window).bind("beforeunload", function () {
    if (preventUnloadPrompt)
    { return; }
    else {
        return confirm("quit??");            
    }
});

感谢任何类型的帮助。 感谢。

2 个答案:

答案 0 :(得分:1)

当一个窗口关闭时,一切,绝对与该窗口相关的所有内容是否可见......等待javascript代码,屏幕上的东西等等都消失了,其中包括处于执行过程中的脚本 - 噗 - 走了nada,基本上取消,中止,停止,停止任何正在运行的脚本。

时间排序要求除关闭窗口外,首先完成所有其他用户启动的操作。

示例:

javascript:
void(window.open("data:text/html,
<html>
<a href=\"javascript:window.open('about:blank');self.close();void(0);\">open & close</a> this link does both<br/>
<a href=\"javascript:self.close();window.open('about:blank');void(0);\">close & open</a> this does close but open fails
</html>
"))

可以使用if(confirm("quit??")){ ... }

对链接进行预处理

测试:

window.navigator.userAgent =
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:11.0) Gecko/20100101 Firefox/11.0

书签:
Open a new browser window on close of current window if user confirms to close current window

答案 1 :(得分:-1)

var answer = confirm("quit??");
if (answer){
    window.open("http://www.google.com/");
}
else{
// do something else
}