javascript中从子窗口刷新父窗口

发布于 2024-09-24 01:17:35 字数 163 浏览 1 评论 0原文

我已经寻找了一段时间,找不到适合我需求的答案。我有一个页面弹出一个窗口(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧 2024-10-01 01:17:35

弹出窗口中的 window.opener 将引用打开窗口的 window 对象,因此您当然应该能够调用 window.opener.location.reload();。只要您不违反同源策略,弹出窗口就可以调用脚本并进行操作父窗口对象上的属性就像父窗口中的代码一样。

这是一个实时示例,演示了子级回调父级上的函数,以及直接操作父级。下面引用了完整的代码。

但那个例子并没有完全按照你所说的做,所以我做了这个好吧,它让子级通过 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.opener in the popup will refer to the window object 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.

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:

<!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 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:

<!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>​
蘑菇王子 2024-10-01 01:17:35

window.parent.top.location = window.parent.top.location; (或者类似的东西就可以了)

window.parent.top.location = window.parent.top.location; (or something similiar to that will do it)

筱果果 2024-10-01 01:17:35

如果其他建议都不起作用(请记住在所有主要浏览器中进行测试),您还可以尝试将父窗口的引用显式传递给子窗口。因为 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 do child = window.open() and then you can do something like child.myParent = window

忆依然 2024-10-01 01:17:35

我在通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文