gpt4 book ai didi

javascript - 监听多个子窗口关闭事件,无法检查多个子窗口是否已关闭

转载 作者:行者123 更新时间:2023-12-03 04:18:57 26 4
gpt4 key购买 nike

页面的父级正在监听多个子窗口关闭事件。如果关闭任何子窗口,则页面将在 GridView 中进行回发。因此,父页面不会重新加载,window_dictionary 也不会“重置”。在下面的代码中,我将 window 对象作为值添加到全局 JavaScript 字典中。该对象包含键(“clickedRow”+记录id的组合),值是窗口对象。

对于第一个窗口关闭事件,它工作得很好,但对于接下来的窗口关闭事件则不行。例如,如果我打开两个子窗口,我可以关闭第一个窗口,并且 __dopostback() 方法将被调用,但是当我关闭剩余的窗口时,则不会调用 __dopostback() 。

我认为 setInterval 调用可能存在问题。然而;我已经被卡住了一段时间,但无法让它按预期工作。

对于每个关闭的窗口,我想要进行回发。

代码

    var openedWindows = {};

setInterval(checkIfWindowIsClose, 40);

function openThisWindow(popLoc, attributes, id) {
var winInst = window.open(popLoc, attributes, id);
openedWindows["clickedRow" + id] = winInst;
}

function checkIfWindowIsClose() {
var id = "";
for (var i in openedWindows) {

if (openedWindows[i].closed) {
// i is the id of your window and here you know that your window with id i has been closed
// here remove also the window from the object otherwise you will keep also the instance of the closed one
console.log(i);
delete openedWindows[i];
id = (' ' + i).slice(1);
id = id.replace("clickedRow", "");

}
}
if (id !== "") {

__doPostBack('upInspectionList.UniqueID', id.toString());
id = "";
}

}

谢谢

最佳答案

创建一个用于存储打开的窗口的对象。在对象内部使用间隔循环来检测窗口是否已关闭。我尝试过,效果很好,当然可以根据您的所有需求更改此示例。通过这种方式,您只需要一个计时器,而不是每次都创建一个新计时器。

HTML:

<div>
<button id="button-1" onclick="openThisWindow(this);">Button 1</button>
<button id="button-2" onclick="openThisWindow(this);">Button 2</button>
<button id="button-3" onclick="openThisWindow(this);">Button 3</button>
</div>

JS:

var openedWindows = {};

setInterval(checkIfWindowIsClose, 4);

function openThisWindow(el) {
var winInst = window.open("http://www.google.it");
openedWindows[el.id] = winInst;
}

function checkIfWindowIsClose() {
for (var i in openedWindows) {
if (openedWindows[i].closed) {
// i is the id of your window and here you know that your window with id i has been closed
// here remove also the window from the object otherwise you will keep also the instance of the closed one
console.log(i);
delete openedWindows[i];
}
}
}

有意义吗?

关于javascript - 监听多个子窗口关闭事件,无法检查多个子窗口是否已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44030323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com