javascript Javascript打开新标签页,等待它关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17171589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:21:30  来源:igfitidea点击:

Javascript open new tab, and wait for it to close

javascript

提问by Mohammad AbuShady

I want to open a new tab for a bank payment on my website using javascript, and without the main window navigating away,

我想使用 javascript 在我的网站上打开一个用于银行付款的新标签,并且没有导航离开主窗口,

when the user is back from the bank payment to a return URL, i want to detect the reply of the return URL in from the other window ( if possible ) or just notify the main window that the transaction is done and it should check the database for updates.

当用户从银行付款返回到返回 URL 时,我想从另一个窗口(如果可能)检测返回 URL 的回复,或者只是通知主窗口交易已完成,它应该检查数据库更新。

I've seen this behaviour on several websites, such as popup->login->popup closes->main window reloadswith the loaded session, The problem is that i don't know what this method is called, so I don't know what keyword I'm looking for.

我在几个网站上看到过这种行为,例如popup->login->popup closes->main window reloads加载会话,问题是我不知道这个方法叫什么,所以我不知道我在找什么关键字。

What I exactly need is either the name of this method, or how is it done ( as a certain keyword in javascript or something )

我真正需要的是这个方法的名称,或者它是如何完成的(作为 javascript 中的某个关键字或其他东西)

Thanks in advance

提前致谢

采纳答案by Niet the Dark Absol

Before closing the popup, try opener.someFunction()to call a function in the window that opened it. Note that this will fail if the user closed the first window, or if the user navigated it to another site, or if the two windows are on different domains for whatever reason.

在关闭弹出窗口之前,尝试opener.someFunction()在打开它的窗口中调用一个函数。请注意,如果用户关闭了第一个窗口,或者用户将其导航到另一个站点,或者两个窗口由于某种原因位于不同的域中,这将失败。

回答by gkalpak

You can open a new page using window.openand then check periodically to see if the window has been closed. The reference to the opened window is returned by window.openand you can check if it has been closed using windowHandle.closed.

您可以打开一个新页面window.open,然后定期检查以查看该窗口是否已关闭。对打开的窗口的引用由 返回window.open,您可以使用 来检查它是否已关闭windowHandle.closed

btn.onclick = function() {
    var win = window.open(
        "http://www.stackoverflow.com", 
        "Secure Payment");
    var timer = setInterval(function() {
        if (win.closed) {
            clearInterval(timer);
            alert("'Secure Payment' window closed !");
        }
    }, 500);
}

See, also, this short demo.

另请参阅此简短演示

For more info on window.openand best practises, take a look at MDN.

有关更多信息window.open和最佳实践,请查看MDN