gpt4 book ai didi

javascript - 从子页面调用时对话框模式窗口未关闭

转载 作者:行者123 更新时间:2023-11-27 23:49:26 25 4
gpt4 key购买 nike

我正在尝试使用 jQuery UI 对话框作为弹出窗口,并且我想将另一个 aspx 页面作为 Jquery UI 对话框的主体。在这里我不想使用 Jquery 按钮选项。在子页面上,我放置了应该关闭模式窗口并刷新父页面的按钮。下面是我一直在尝试实现的代码,但由于某些原因我收到了 js 错误消息。我在这里错过了什么吗?

父页面:aspx页面

  <div>
<div id="dialog" title="This is Pop up ">
<div class="modal">
<div class="modal-body">
<iframe style="width: 100%; height: 100%;" src="childPage.aspx" runat="server" frameborder="0"></iframe>
</div>
</div>
</div>
<input type="button" value="open" id="OpenDialog"/>
</div>

Jquery 代码:父页面

 $(function () {
var dialog
dialog = $("#dialog").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
});
$("#OpenDialog").button().on("click", function () {
dialog.dialog("open");
});
});

子页面:

 <input type="button" id="btnCloseChildPageRefreshParent" value="Close and Refresh Parent Page" />

子页面Js代码:

   $(function () {
$('#btnCloseChildPageRefreshParent').on('click', function () {
refreshParent();
open(location, '_self').close();
});

function refreshParent() {
window.opener.location.reload();
}
});

最佳答案

这是一个 iframe,因此您需要使用 window.parent ( see the MDN documentation here ) 而不是 window.opener。它不是一个新窗口,而是一个框架,因此没有开启器。

注意frame和parent的域必须匹配,否则由于跨域安全限制,调用会失败。

下面的代码示例将打印出 window.opener 的值以及调用 window.parent.location.reload 生成的错误来说明这一点。

function log (o) {
var el = document.createElement('div');
el.innerHTML = o;
document.body.appendChild(el);
}

document.getElementById('button').onclick = function () {
//This line could be used if the domain of the frame and parent match
//window.parent.location.reload();

log('window.opener is: ' + window.opener);

try {
window.parent.location.reload();
}
catch (e) {
log('Attempted to call window.parent.location.reload(): ' + e);
}
}
<button id="button">Reload Parent</button>

关于javascript - 从子页面调用时对话框模式窗口未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32851806/

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