I w...">

Get link to _blank target window on submit form

I have a form that opens a new window for submission:

<form id="form-id" target="_blank"> 

I want to access the newly created window through javascript, without manually creating a unique name for the target, and without resorting to an alternative method of opening the window .

There seems to be an easy way to do this, but I have not been able to find one that will work in my specific situation.

+6
source share
2 answers

I am not 100% sure that this works in all browsers, but Firefox seems to set window.opener when the target attribute on <a> or <form> causes a new window to open. Thus, you can go in a different direction and find the original window from a new one (provided that you manage the code there, if not, I can’t imagine that you could do a lot with the link to the window).

Of course, one of the things that code in a new window can do is call the function in the old window, passing its own window link.

Thus, if you have:

 <form action=whatever target=_blank> 

on the original page, then the page that ends in a recently opened window can do this:

 <head> <script> if (window.opener) { window.opener.announceWindow( window ); } </script> 

This suggests that announceWindow() is a function on the original page, something like:

 function announceWindow( win ) { // do stuff with "win", a newly-opened window } 
+4
source

Instead of _blank you can use the name in a new window.

  <form id="form-id" target="newName"> 

Then you can use it in JS by doing:

 var newWindow = window.open(null, 'newName'); 
+4
source

All Articles