javascript 在javascript中从子窗口刷新父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3762329/
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
Refresh the parent window from the child window in javascript
提问by Isaac Levin
I have looked for awhile and cannot find an answer that fits my needs. I have a page that pops a window (window.open), logs the user in (creates a cookie, set session) then redirects to another page. While the modal is redirecting, I would like to refresh the parent page, so all the good stuff that I just did will be recognized by the parent. I have tried window.opener and stuff like that. Can someone help me out a little? Thanks
我已经找了一段时间,找不到适合我需要的答案。我有一个页面弹出一个窗口(window.open),让用户登录(创建一个cookie,设置会话)然后重定向到另一个页面。当模态重定向时,我想刷新父页面,所以我刚刚做的所有好东西都会被父页面识别。我试过 window.opener 之类的东西。有人可以帮我一下吗?谢谢
回答by T.J. Crowder
window.openerin the popup will refer to the windowobject of the opening window, so of course you should be able to call window.opener.location.reload();. Provided you don't run afoul of the Same Origin Policy, the popup window can call scripts and manipulate properties on the parent window object just like code in the parent can.
window.opener在弹出窗口中将引用window打开窗口的对象,所以当然你应该可以调用window.opener.location.reload();. 如果您不违反同源策略,弹出窗口可以调用脚本并操作父窗口对象上的属性,就像父窗口对象中的代码一样。
Here's a live exampledemonstrating the child calling back to a function on the parent, and also manipulating the parent directly. The full code for this is quoted below.
这是一个现场示例,演示子级回调父级上的函数,并直接操作父级。下面引用了完整的代码。
But that example didn't do exactlywhat you said, so I've done this one as well, which has the child refreshing the parent page via window.opener.location.reload().
但是那个例子并没有完全按照你说的做,所以我也做了这个,它让孩子通过window.opener.location.reload().
Parent code of first live example:
第一个实时示例的父代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Parent Window</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<input type='button' id='btnOpen' value='Open Window'>
<div id='display'></div>
</body>
<script type='text/javascript'>
(function() {
var btnOpen = document.getElementById('btnOpen');
btnOpen.onclick = btnOpenClick;
function btnOpenClick() {
window.open('http://jsbin.com/ehupo4/2');
}
// Make the callback function available on our
// `window` object. I'm doing this explicitly
// here because I prefer to export any globals
// I'm going to create explicitly, but if you
// just declare a function in a script block
// and *not* inside another function, that will
// automatically become a property of `window`
window.callback = childCallback;
function childCallback() {
document.getElementById('display').innerHTML =
'Got callback from child window at ' + new Date();
}
})();
</script>
</html>?
(You don't haveto use a scoping function as I did above, but it's useful to keep your globals contained.)
(你不会有像我上面那样使用范围界定功能,但要保持你的全局变量包含它是非常有用的。)
Child code of first live example:
第一个实例的子代码:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Child Window</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<p>I'm the child window</p>
</body>
<script type='text/javascript'>
if (window.opener) {
// Call the provided callback
window.opener.callback();
// Do something directly
var p = window.opener.document.createElement('p');
p.innerHTML = "I was added by the child directly.";
window.opener.document.body.appendChild(p);
}
</script>
</html>?
回答by automagic
window.parent.top.location = window.parent.top.location; (or something similiar to that will do it)
window.parent.top.location = window.parent.top.location; (或类似的东西会做到这一点)
回答by 7wp
If none of the other suggestions work (Remember to test in all major browsers), you can also try passing a reference of the parent window to the child window explicitly. Because window.open()should return a reference to the child window.. so you can do child = window.open()and then you can do something like child.myParent = window
如果其他建议都不起作用(请记住在所有主要浏览器中进行测试),您还可以尝试将父窗口的引用显式传递给子窗口。因为window.open()应该返回对子窗口的引用..所以你可以做 child = window.open()然后你可以做类似的事情child.myParent = window
回答by ahimmelstoss
I had the same issue with submitting a form in a modal via ajax and need the page underneath (parent page) to reload. window.location.reload();worked for me.
我在通过 ajax 以模态形式提交表单时遇到了同样的问题,需要重新加载下面的页面(父页面)。window.location.reload();对我来说有效。

