简体   繁体   English

如何从 iFrame 中重新加载主页

[英]How to reload Main Page from within an iFrame

Within my scenario, I have a button within an iframe section of my page that performs some database processing.在我的场景中,我的页面的 iframe 部分中有一个按钮,用于执行一些数据库处理。

What I need is a means of performing a page refresh of the main page, when this button within the iframe is pressed.我需要的是一种在 iframe 中的这个按钮被按下时执行主页面页面刷新的方法。

I am seeking some JavaScript code that I can trigger within the iframe, that will reload the main window holding the iframe.我正在寻找一些可以在 iframe 中触发的 JavaScript 代码,这将重新加载包含 iframe 的主窗口。

window.top.location.reload();

如果父 iframe 域和子 iframe 域不同,您将收到跨窗口安全错误,在这种情况下,您可以尝试使用以下内容:

window.parent.location = document.referrer;
window.parent.location.reload();

We can easily achieve the facing goal by set target="_parent" to the Links which inside the iframe.我们可以通过将 target="_parent" 设置为 iframe 内的 Links 来轻松实现面对的目标。

Just like the following demo shows:就像下面的演示一样:

<!--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>

define allFrame variable on your top frame:在顶部框架上定义 allFrame 变量:

var allFrame=1

and on all your frames check this function并在所有框架上检查此功能

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM