如果用户确认关闭当前窗口,则在关闭当前窗口时打开一个新的浏览器窗口 [英] Open a new browser window on close of current window if user confirms to close current window

查看:86
本文介绍了如果用户确认关闭当前窗口,则在关闭当前窗口时打开一个新的浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,如果用户尝试关闭当前浏览器窗口,则应该在他身上显示一个确认框.如果他确认关闭窗口,则该窗口应关闭并使用不同的URL打开新窗口.

I have a scenario where if the user tries to close the current browser window then he should be displayed a confirm box. If he confirms to close the window then this window should close and opening the new window with different url.

我尝试使用以下代码来显示确认框,但是如果用户确认关闭当前窗口,我将无法弄清楚如何打开新窗口.

I tried use the following code by which I'm able to show the confirm box but I'm unable to figure out how to open the new window if user confirms to close the current.

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??");            
    }
});

任何类型的帮助,我们感激不尽. 谢谢.

Any type of help is appreciated. Thanks.

推荐答案

当窗口关闭时,与该窗口关联的所有内容,无论是可见的还是不可见的,绝对是...与之相关的所有JavaScript代码,屏幕上的内容等都消失了,包括执行中的脚本-poof-没用的nada,基本上是取消,中止,暂停,停止任何正在运行的脚本.

When a window closes, everything, absolutely everything associated with that window whether visible or not ... pending javascript code, stuff on screen, etc. vanishes and that includes the very scripts that are in the middle of execution - poof - gone nada, basically canceling, aborting, halting, stopping any running script.

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

The temporal sequencing requires that, except for closing a window, all other user initiated operations be completed first.

示例:

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

书签:
查看全文

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