【问题标题】:How to reload Main Page from within an iFrame如何从 iFrame 中重新加载主页
【发布时间】:2009-06-05 06:05:41
【问题描述】:

在我的场景中,我的页面的 iframe 部分中有一个按钮,用于执行一些数据库处理。

当按下 iframe 中的此按钮时,我需要一种方法来执行主页的页面刷新。

我正在寻找一些可以在 iframe 中触发的 JavaScript 代码,这些代码将重新加载包含 iframe 的主窗口。

【问题讨论】:

    标签: javascript


    【解决方案1】:
    window.top.location.reload();
    

    【讨论】:

    • +1 用于使用 window.top 确保主页被定位。
    • 感谢重新加载答案 - 在 ie6 中使用 javascript 执行页面刷新怎么样?
    • 如果主页在框架集中会发生什么??
    • @rahul,这将“重新加载主窗口”(即最顶部),这是扁桃体要求的。
    【解决方案2】:

    如果父 iframe 域和子 iframe 域不同,则会出现跨窗口安全错误,在这种情况下,您可以尝试使用以下方法:

    window.parent.location = document.referrer;
    

    【讨论】:

    • 你是对的。当使用window.top.location.reload(); 代码从不同来源的 iframe 中重新加载父页面时,这会产生安全错误:Blocked a frame with origin "iframe-url-here" from accessing a cross-origin frame. 但是您的示例工作得很好!谢谢你的回答!
    • Failed to set the 'href' property on 'Location': The current window does not have permission to navigate the target frame to ''
    【解决方案3】:
    window.parent.location.reload();
    

    【讨论】:

    • 重新加载框架父级而不是“主页”,仅遍历一个级别而无需额外的 .parent 规范,.top 正如 Matthew 建议的那样,无论级别数如何,都会重新加载“主页”
    • 最初的问题描述并没有暗示有任何超过一层的框架 - 也不是 if 有超过一层,希望最顶层的框架所有这些级别都是重新加载的级别。如果您的主页碰巧被加载到某个其他站点的框架中,我的方法重新加载该主页——而重新加载window.top则不会。跨度>
    【解决方案4】:

    我们可以通过将 target="_parent" 设置为 iframe 内的链接来轻松实现面对目标。

    就像下面的演示所示:

    <!--ParentPage.htm-->
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
    Parent Page
    <iframe src="FramePage.htm"></iframe>
    </body>
    </html>
    
    
    <!--FramePage.htm-->
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    </head>
    <body>
    <a href="http://www.g.cn" target="_parent">hello</a>
    </body>
    </html>
    

    【讨论】:

      【解决方案5】:

      在顶部框架上定义 allFrame 变量:

      var allFrame=1
      

      并在所有框架上检查此功能

      if(top.allFrame === undefined)
      {
          window.parent.location = "your website top frame";
      }
      

      【讨论】:

        【解决方案6】:
        window.opener.top.location.reload();
        

        【讨论】:

          【解决方案7】:

          如果您使用 aspx C# 编写 Page 代码,您可以查看代码:

          ClientScript.RegisterStartupScript(this.GetType(), "LoadParent", "<script language=javascript>window.parent.location.reload();</script>");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-22
            • 2014-09-23
            • 2011-12-20
            • 2013-06-22
            • 1970-01-01
            • 1970-01-01
            • 2013-11-04
            • 1970-01-01
            相关资源
            最近更新 更多