在javascript中从子窗口刷新父窗口 [英] Refresh the parent window from the child window in javascript

查看:92
本文介绍了在javascript中从子窗口刷新父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找了一段时间,找不到符合我需要的答案。我有一个页面弹出一个窗口(window.open),记录用户(创建一个cookie,设置会话)然后重定向到另一个页面。虽然modal是重定向的,但我想刷新父页面,所以我刚才所做的所有好东西都会被父级识别。我试过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

推荐答案

window.opener 将引用开头的窗口对象窗口,所以当然你应该能够调用 window.opener.location.reload(); 。如果您未与同源策略发生冲突,则弹出窗口可以调用脚本并进行操作父窗口对象上的属性就像父窗口中的代码一样。

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.

但是这个例子并没有完全 你所说的,所以我也完成了这个,它让孩子通过 window.opener.location刷新父页面.reload()

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().

第一个实例的父代码:

<!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.)

第一个实例的子代码:

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

这篇关于在javascript中从子窗口刷新父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆