Javascript 在iframe中提交后如何刷新iframe父页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1860673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh iframe parent page after a submit in the iframe?
提问by Amr Elgarhy
i have iframe inside a page, and this iframe contain a submit button which will do some function.
我有一个页面内的 iframe,这个 iframe 包含一个提交按钮,它将执行一些功能。
What i want is: after the iframe submit finish, call the parent page to refresh.
我想要的是:iframe 提交完成后,调用父页面刷新。
i know how to refresh it:
我知道如何刷新它:
parent.location.reload();
But i don't know how to do that after submit finish.
但我不知道提交完成后该怎么做。
采纳答案by Amr Elgarhy
I solved this problem by adding this line of code in the code behind after all my methods finished:
我通过在所有方法完成后在后面的代码中添加这行代码来解决这个问题:
ScriptManager.RegisterStartupScript(this, typeof(string), "script",
"<script type=text/javascript>
parent.location.href = parent.location.href;</script>", false);
And the reason i didn't write parent.location.reload()and wrote parent.location.href = parent.location.hrefis to don't send the data twice to server as i want a new fresh page load.
我没有写parent.location.reload()和写的原因parent.location.href = parent.location.href是不要将数据发送到服务器两次,因为我想要一个新的页面加载。
回答by Josh Stodola
Use an onloadevent handler on your iframe. When it fires after the submit, execute your top.location.reload...
onload在 iframe 上使用事件处理程序。当它在提交后触发时,执行你的top.location.reload...
// In parent
window.onload = function() {
document.getElementById("MY_IFRAME").onload = function() {
top.location.reload();
}
}
回答by sangam
If you only want to refresh some parts of the parent page and won't need to refresh the whole page, and most of the times this is the case since this approach doesn't reload the iframe itself, you can just call a javascript function from the child page.
如果您只想刷新父页面的某些部分而不需要刷新整个页面,并且大多数情况下都是这种情况,因为这种方法不会重新加载 iframe 本身,您只需调用 javascript 函数从子页面。
window.parent.myFunctionInParent('my argument');
The resource that guides you is here: Refresh parent page partially from iframe
指导您的资源在这里:从 iframe 部分刷新父页面
Thanks.
谢谢。
回答by Diodeus - James MacFarlane
Submitting a form is the FINAL action on the document in the Iframe. Once you submit a form, that page has been "submitted" and is no longer active. If you refresh the parent first, you lose the Iframe. The moment you submit in the Iframe, you can no longer talk to the parent.
提交表单是对 Iframe 中文档的最终操作。提交表单后,该页面已“提交”并且不再处于活动状态。如果您先刷新父级,则会丢失 Iframe。在 Iframe 中提交的那一刻,您就无法再与父级对话。
You can do one of a few things here:
您可以在此处执行以下操作之一:
- Target the PARENT when you submit the form, then the parent will have whatever you want in a NEW iframe on that page.
- Have the resulting page in the iframe reload the parent using JavaScript on the result page.
- 提交表单时定位到 PARENT,然后父级将在该页面上的新 iframe 中拥有您想要的任何内容。
- 让 iframe 中的结果页面在结果页面上使用 JavaScript 重新加载父页面。

