5

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

4

4 回答 4

10

window.opener在弹出窗口中将引用window打开窗口的对象,因此您当然应该可以调用window.opener.location.reload();. 如果您不违反Same Origin Policy,弹出窗口可以调用脚本并操作父窗口对象上的属性,就像父窗口中的代码一样。

这是一个现场示例,演示了子调用父函数的函数,并直接操作父函数。下面引用了完整的代码。

但是那个例子并没有完全按照你说的做,所以我也做了这个,它让孩子通过刷新父页面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>​
于 2010-09-21T16:24:26.460 回答
1

window.parent.top.location = 窗口.parent.top.location; (或类似的东西会这样做)

于 2010-09-21T16:25:09.950 回答
1

如果其他建议都不起作用(请记住在所有主要浏览器中进行测试),您还可以尝试将父窗口的引用显式传递给子窗口。因为window.open()应该返回对子窗口的引用..所以你可以做 child = window.open() 然后你可以做类似的事情child.myParent = window

于 2010-09-21T16:29:42.857 回答
0

我在通过 ajax 以模式提交表单时遇到了同样的问题,需要重新加载下面的页面(父页面)。window.location.reload();为我工作。

于 2015-04-09T21:30:04.523 回答