我已经看过了一段时间,并不能找到一个适合我的需要的答案。我有一个页面弹出一个窗口(window.open),登录用户(创建一个cookie,设置会话),然后重定向到另一个页面。当模式重定向时,我想刷新父页面,所以我刚做的所有好东西都会被父页面识别。我试过window.opener和类似的东西。有人能帮我一点吗?谢谢刷新从子窗口的父窗口在JavaScript
回答
window.opener
在弹出窗口中会提到window
对象的开启窗口,所以当然你应该可以调用window.opener.location.reload();
。只要你不触犯Same Origin Policy的运行,在弹出的窗口中可以调用脚本和操作父窗口对象就像家长可以代码的属性。
这里有一个live example证明孩子叫回功能父,也可以直接操纵父母。下面引用了完整的代码。
但是这个例子并没有做正是你说的话,所以我done this one as well,其中有孩子通过window.opener.location.reload()
刷新父页面。第一个活生生的例子的
父代码:
<!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>
(你不有正如我上面做了使用范围界定功能,但要保持你的全局变量包含的是有用的。)
第一个实例的子代码:
<!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>
window.parent.top.location = window.parent.top.location; (或类似的东西来,这将做到这一点)
window.opener也许更好 – automagic 2010-09-21 16:26:05
如果没有其他建议的工作(请务必测试在所有主要的浏览器),你也可以尝试通过父窗口的参考子窗口明确。由于window.open()
应该返回一个参考子窗口..所以你可以做child = window.open()
,然后你可以这样做child.myParent = window
非常真实。 'window.opener'是可靠的禁止SOP,这也会与SOP相冲突,但是很好的选择。你必须处理一点时间,这是我喜欢让孩子打电话给父母的部分原因。 – 2010-09-21 16:34:29
我有同样的问题,在通过AJAX模态提交表单,需要下面的页面(父页面)重新加载。 window.location.reload();
为我工作。
- 1. 从子窗口刷新父窗口
- 2. 在父窗口刷新子窗口
- 3. 关闭子窗口刷新父窗口
- 4. Javascript:如何从子窗口刷新父窗口
- 5. Javascript getElementById从父窗口到子窗口
- 6. 从子窗口刷新父项?
- 7. 如何从父窗口刷新子弹出窗口?
- 8. 刷新父窗口在IE8
- 9. 刷新父窗口的Javascript起源
- 10. 防止父窗口刷新打开子窗口
- 11. 刷新父窗口时子窗口未关闭
- 12. 关闭子窗口时刷新父窗口
- 13. Silverlight子窗口父窗口
- 14. 父窗口和子窗口
- 15. 从父窗口关闭子窗口
- 16. 从子窗口关闭父窗口
- 17. 从弹出窗口刷新主窗口
- 18. JavaScript的 - 重新加载父窗口,如果子窗口关闭
- 19. Silverlight 4和当父窗口关闭时刷新父窗口
- 20. 如何从iframe刷新父窗口?
- 21. 处理程序在刷新父窗口后弹出窗口
- 22. 在asp.net中刷新父窗口弹出窗口c#
- 23. 的Javascript:从父窗口
- 24. 如何在父窗口关闭时从子窗口访问父窗口方法?
- 25. 从父窗口调用子窗口函数javascript
- 26. 使用javascript阻止从子窗口访问父窗口
- 27. JavaScript将数据从子窗口传递给父窗口,IE bug?
- 28. 如何使用javascript从父窗口获取子窗口url
- 29. 关闭servlet中的弹出窗口并刷新父窗口
- 30. 如何父窗口的子窗口
您的意思是“刷新从孩子的javascript父”?我喜欢来自新墨西哥州的青辣椒,但我不知道在这种情况下辣椒是什么。 – 2010-09-21 16:23:19
window.parent是不是? 'window.parent.frames [0]'或类似的https://developer.mozilla.org/en/DOM – 2010-09-21 16:23:51
window.opener通常是你要在子窗口上调用的东西。你可以用你的脚本显示你的脚本来打开这个新窗口吗? – 2010-09-21 16:25:35