【发布时间】:2009-06-05 06:05:41
【问题描述】:
在我的场景中,我的页面的 iframe 部分中有一个按钮,用于执行一些数据库处理。
当按下 iframe 中的此按钮时,我需要一种方法来执行主页的页面刷新。
我正在寻找一些可以在 iframe 中触发的 JavaScript 代码,这些代码将重新加载包含 iframe 的主窗口。
【问题讨论】:
标签: javascript
在我的场景中,我的页面的 iframe 部分中有一个按钮,用于执行一些数据库处理。
当按下 iframe 中的此按钮时,我需要一种方法来执行主页的页面刷新。
我正在寻找一些可以在 iframe 中触发的 JavaScript 代码,这些代码将重新加载包含 iframe 的主窗口。
【问题讨论】:
标签: javascript
window.top.location.reload();
【讨论】:
如果父 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 ''
window.parent.location.reload();
【讨论】:
window.top则不会。跨度>
我们可以通过将 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>
【讨论】:
在顶部框架上定义 allFrame 变量:
var allFrame=1
并在所有框架上检查此功能
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>");
【讨论】: