2010-09-21 68 views
5

我已经看过了一段时间,并不能找到一个适合我的需要的答案。我有一个页面弹出一个窗口(window.open),登录用户(创建一个cookie,设置会话),然后重定向到另一个页面。当模式重定向时,我想刷新父页面,所以我刚做的所有好东西都会被父页面识别。我试过window.opener和类似的东西。有人能帮我一点吗?谢谢刷新从子窗口的父窗口在JavaScript

+1

您的意思是“刷新从孩子的javascript父”?我喜欢来自新墨西哥州的青辣椒,但我不知道在这种情况下辣椒是什么。 – 2010-09-21 16:23:19

+0

window.parent是不是? 'window.parent.frames [0]'或类似的https://developer.mozilla.org/en/DOM – 2010-09-21 16:23:51

+0

window.opener通常是你要在子窗口上调用的东西。你可以用你的脚本显示你的脚本来打开这个新窗口吗? – 2010-09-21 16:25:35

回答

10

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>​ 
1

window.parent.top.location = window.parent.top.location; (或类似的东西来,这将做到这一点)

+0

window.opener也许更好 – automagic 2010-09-21 16:26:05

1

如果没有其他建议的工作(请务必测试在所有主要的浏览器),你也可以尝试通过父窗口的参考子窗口明确。由于window.open()应该返回一个参考子窗口..所以你可以做child = window.open(),然后你可以这样做child.myParent = window

+0

非常真实。 'window.opener'是可靠的禁止SOP,这也会与SOP相冲突,但是很好的选择。你必须处理一点时间,这是我喜欢让孩子打电话给父母的部分原因。 – 2010-09-21 16:34:29

0

我有同样的问题,在通过AJAX模态提交表单,需要下面的页面(父页面)重新加载。 window.location.reload();为我工作。