javascript中从子窗口刷新父窗口
我已经寻找了一段时间,找不到适合我需求的答案。我有一个页面弹出一个窗口(window.open),让用户登录(创建cookie,设置会话),然后重定向到另一个页面。当模态重定向时,我想刷新父页面,这样我刚刚做的所有好东西都会被父页面识别。我尝试过 window.opener 之类的东西。有人可以帮我一点吗?谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
弹出窗口中的
window.opener
将引用打开窗口的window
对象,因此您当然应该能够调用window.opener.location.reload();
。只要您不违反同源策略,弹出窗口就可以调用脚本并进行操作父窗口对象上的属性就像父窗口中的代码一样。这是一个实时示例,演示了子级回调父级上的函数,以及直接操作父级。下面引用了完整的代码。
但那个例子并没有完全按照你所说的做,所以我做了这个好吧,它让子级通过 window.opener.location.reload() 刷新父页面。
第一个实时示例的父代码:(
您不必像上面那样使用作用域函数,但保留全局变量很有用。)
第一个实时示例的子代码:
window.opener
in the popup will refer to thewindow
object of the opening window, so of course you should be able to callwindow.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.Here's a live example demonstrating 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 exactly what you said, so I've done this one as well, which has the child refreshing the parent page via
window.opener.location.reload()
.Parent code of first live example:
(You don't have to use a scoping function as I did above, but it's useful to keep your globals contained.)
Child code of first live example:
window.parent.top.location = window.parent.top.location; (或者类似的东西就可以了)
window.parent.top.location = window.parent.top.location; (or something similiar to that will do it)
如果其他建议都不起作用(请记住在所有主要浏览器中进行测试),您还可以尝试将父窗口的引用显式传递给子窗口。因为
window.open()
应该返回对子窗口的引用..所以你可以这样做child = window.open()
然后你可以做类似child.myParent = 窗口
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 dochild = window.open()
and then you can do something likechild.myParent = window
我在通过 ajax 在模式中提交表单时遇到了同样的问题,并且需要重新加载下面的页面(父页面)。
window.location.reload();
为我工作。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.