从JS中的iframe重新加载页面

发布于 2025-02-08 16:11:34 字数 937 浏览 2 评论 0原文

我有一个iframe,我想通过它刷新主页。我尝试了诸如window.location.reload()等的函数。但是看来它不起作用。 我需要将重新加载函数放在不在主页上的iframe上。 我可以得到document.referrer获得主页。

iframe codesandbox 请参阅此

>

 <div class="iframe-container">
      <iframe class="iframe-layout" src="https://zqdqmn.csb.app/"></iframe>
  </div>

/ strong > iframe part

  <script>
  
    function redirectToGoogle() {
      const mainUrl = document?.referrer

      window.location.href = `${mainUrl}`

      // window.open(mainUrl,"_self")
    }

  </script>
</body>

I have an iframe and I want to refresh the main page through it. I've tried functions such as window.location.reload() etc.. but it seems it doesn't work.
I need to put the reload function on the iframe not on the main page.
I could get as much as document.referrer to get the main page.

Iframe codesandbox
SEE THIS

Main page codesandbox SEE THIS

 <div class="iframe-container">
      <iframe class="iframe-layout" src="https://zqdqmn.csb.app/"></iframe>
  </div>

Iframe Part

  <script>
  
    function redirectToGoogle() {
      const mainUrl = document?.referrer

      window.location.href = `${mainUrl}`

      // window.open(mainUrl,"_self")
    }

  </script>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有心的人 2025-02-15 16:11:34

要与横断面互动,使我们成为 窗口。 postmessage 使用onMessageaddeventListener(“消息”,...)

codesandbox上的示例使用特定的来源,而此处的示例显示了如何处理它,如果目标和/或源窗口具有'null'作为Origin。

window.addEventListener("message", async ({ data, origin, source }) => {
  // another check would be a check for the origin, but since the iframe was created with srcdoc, the origin is null
  console.log("Origin:",origin);
  // only react if the source window is the frame
  if (source == frameToListen.contentWindow) {
    try { // in case of "data == null"
      console.log("data", data);
      // wait for 5 seconds before reload
      if (data.reload) {
        await new Promise(r => setTimeout(r,5000));
        console.log("reload..?");
        window.location.reload();
      }
    } catch (e) {
      console.error(e);
    }
  }
});

// Generate dummies in the iframe to send some events
// The only way to contact a window with an origin of "null" is by "*"
frameToListen.srcdoc = `<script>function send(obj) {
    window.parent.postMessage(obj, '*');
    // avoid unexpected end of input in stacksnippet-parser
  }</scr${""}ipt><input type="button" value="undefined" onclick="send()">
  <input type="button" value="null" onclick="send(null)">
  <input type="button" value="{ reload: true }" onclick="send({ reload: true })">
  <input type="button" value="{ reload: false }" onclick="send({ reload: false })">
  <input type="button" value="[]" onclick="send([])">`;
<iframe id="frameToListen" style="width: 100%;"></iframe>

To interact with cross origin pages make us of window.postMessage and MessageEvents using onmessage or addEventListener("message", ...).

The example on codesandbox shows how to make use with a specific origin, while this example here shows, how to handle it, if the target and/or source window have 'null' as origin.

window.addEventListener("message", async ({ data, origin, source }) => {
  // another check would be a check for the origin, but since the iframe was created with srcdoc, the origin is null
  console.log("Origin:",origin);
  // only react if the source window is the frame
  if (source == frameToListen.contentWindow) {
    try { // in case of "data == null"
      console.log("data", data);
      // wait for 5 seconds before reload
      if (data.reload) {
        await new Promise(r => setTimeout(r,5000));
        console.log("reload..?");
        window.location.reload();
      }
    } catch (e) {
      console.error(e);
    }
  }
});

// Generate dummies in the iframe to send some events
// The only way to contact a window with an origin of "null" is by "*"
frameToListen.srcdoc = `<script>function send(obj) {
    window.parent.postMessage(obj, '*');
    // avoid unexpected end of input in stacksnippet-parser
  }</scr${""}ipt><input type="button" value="undefined" onclick="send()">
  <input type="button" value="null" onclick="send(null)">
  <input type="button" value="{ reload: true }" onclick="send({ reload: true })">
  <input type="button" value="{ reload: false }" onclick="send({ reload: false })">
  <input type="button" value="[]" onclick="send([])">`;
<iframe id="frameToListen" style="width: 100%;"></iframe>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文