最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

window.location - Refresh the parent window from the child window in javascript - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Sep 21, 2010 at 16:24 7wp 12.7k20 gold badges79 silver badges107 bronze badges asked Sep 21, 2010 at 16:20 Isaac LevinIsaac Levin 511 gold badge1 silver badge2 bronze badges 3
  • 1 Did you mean "Refresh the parent from the CHILD javascript"? I like green chili, being from New Mexico, but I don't know what a chili is in this context. – Brian Stinar Commented Sep 21, 2010 at 16:23
  • window.parent isn't it? window.parent.frames[0] or similar developer.mozilla/en/DOM – David Yell Commented Sep 21, 2010 at 16:23
  • window.opener is usually what you will call on the child window. Can you show your script your using to open this new window? – Ryan Ternier Commented Sep 21, 2010 at 16:25
Add a ment  | 

4 Answers 4

Reset to default 10

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./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./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 bee 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./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; (or something similiar to that will do it)

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

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.

发布评论

评论列表(0)

  1. 暂无评论