gpt4 book ai didi

javascript - 如何在获得响应后刷新加载 iFrame 的页面

转载 作者:太空宇宙 更新时间:2023-11-03 14:41:41 25 4
gpt4 key购买 nike

我有一个名为 default.aspx 的 C# 页面,我从中打开 iFrame。一旦我得到响应,我就在我的 session 中设置值。但是由于缺少页面刷新,我无法获取页面上显示的值并隐藏 iframe。

我用了很多可能的Js函数:

window.parent.location = document.referrer位置.top.reload

我希望在收到 Iframe 的响应后刷新我的页面

最佳答案

您可以使用window.postMessageiframe 的父级通信。

在您的 iframe 中:

window.parent.postMessage("loaded");

在你的父 aspx 中:

window.addEventListener("loaded", receiveMessage, false);

function receiveMessage(event) {
// do reload
window.location.reload();
}

您可以在 MDN 中阅读有关此 API 的更多信息.

长话短说:

The window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.

Normally, scripts on different pages are allowed to access each other if and only if the pages they originate from share the same protocol, port number, and host (also known as the "same-origin policy"). window.postMessage() provides a controlled mechanism to securely circumvent this restriction (if used properly).

Broadly, one window may obtain a reference to another (e.g., via targetWindow = window.opener), and then dispatch a MessageEvent on it with targetWindow.postMessage(). The receiving window is then free to handle this event as needed. The arguments passed to window.postMessage() (i.e., the “message”) are exposed to the receiving window through the event object.

关于javascript - 如何在获得响应后刷新加载 iFrame 的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537795/

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