简体   繁体   中英

return back to parent window using javascript

I am popping out a child window from parent window.Now, in child window, I have a html form that a user will fill and submit and request will go on server site.Now, I wanted response to this request redirected to parent window and my child window automatically closed out.Hope I am clear enough. If not let me know. thank you in advance.

If the pop-up window is created using window.open , then the pop-up window can access the opening window using window.opener .

On the main window, you have to create a function to receive data from the popup, something like this:

window.receiveDataFromPopup = function(data) {
    // do things with data.
};

Then in the pop-up window, you can call that function on the opener like this:

window.opener.receiveDataFromPopup(data);

To close the window, just use

window.close();

Check jQuery Popup Window Return Value to Parent

basically the logic can be:

  1. You response returns a page that contains a hidden-div that contains the message you want to display (hidden with CSS set display:none; )

  2. That child page returned also contains some JavaScript that fills in the parent window with the DIV content, parent/* or opener */.document.getElementById(...).innerHTML = ...;
    or calls a function on the parent to tell it it's now complete, like window.opener/*or parent*/.formIsComplete(); where formIsComplete() is a JavaScript function in the parent document/window.

  3. The last line of the JavaScript on the child window, will close itself, as simple as window.close(); .

<script type='text/javascript'>
  window.opener.location.reload();    
  window.close();
</script>

There is a way to access the parent window from the child. It is

window.opener //use window.opener to access the parent window.

So in the child window, once your form submission completes, just call

window.opener.yourFunc() //Your func is a function on parent. Call this from the child.

Once this function is called close your child window using

window.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM