obtain reference to _blank target window on form submit
Asked Answered
M

3

8

I have a form that is opening a new window on submit:

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

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

It seems like there has to be an easy way of doing this but I wasn't able to find one that will work in my specific situation.

Mireielle answered 26/11, 2012 at 17:15 Comment(1)
Why would someone vote this question down? At least a comment would be helpful. Don't assume that I haven't looked into this; and just because you don't understand the circumstances leading to the question, that does not reflect the reasonableness of asking it.Mireielle
O
5

I'm not 100% sure this works in all browsers, but Firefox seems to set window.opener when a target attribute on an <a> or <form> causes a new window to open. Thus, you can go the other direction and find the original window from the new one (assuming you control the code there; if not, well I can't imagine you could do much with the window reference anyway).

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

Thus, specifically, if you have:

<form action=whatever target=_blank>

on the original page, then the page that ends up in the newly-opened window can do this:

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

That assumes announceWindow() is a function on the original page, something perhaps like:

function announceWindow( win ) {
  // do stuff with "win", a newly-opened window
}
Ownership answered 26/11, 2012 at 18:38 Comment(3)
Thank you Pointy! That is a very interesting approach to solving this.Mireielle
@Mireielle you're welcome! Like I said, I'm not super-sure this is widely supported; it may be however, since the opener thing is really old. I didn't know that links/forms with "target" would set it until I tried it for your question :-)Ownership
This does seem to be the best way of dealing with this situation. It looks like although window.opener is not part of an official specification, it's supported by all major browsers and is unlikely to go away... developer.mozilla.org/en-US/docs/DOM/window.openerMireielle
D
5

Instead of _blank then you can use name your new window.

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

Then you can use it in JS by doing:

var newWindow = window.open(null, 'newName');
Dispute answered 26/11, 2012 at 17:17 Comment(3)
Thanks Neal, but as I mention in the question, for the specific situation I have to use target="_blank", ideally "without manually generating a unique name for target, and without resorting to an alternative method for opening the window."Mireielle
Three people have up-voted this answer, despite what I've clearly said in the question, as well as commented here? Under normal circumstances, I would agree that this would be the best approach, which is why I specifically ruled it out when I asked for help. Can what I am trying to do be done? Thanks.Mireielle
@Ownership Thank you for the humor, I literally laughed out loud when I read your comment.Mireielle
O
5

I'm not 100% sure this works in all browsers, but Firefox seems to set window.opener when a target attribute on an <a> or <form> causes a new window to open. Thus, you can go the other direction and find the original window from the new one (assuming you control the code there; if not, well I can't imagine you could do much with the window reference anyway).

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

Thus, specifically, if you have:

<form action=whatever target=_blank>

on the original page, then the page that ends up in the newly-opened window can do this:

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

That assumes announceWindow() is a function on the original page, something perhaps like:

function announceWindow( win ) {
  // do stuff with "win", a newly-opened window
}
Ownership answered 26/11, 2012 at 18:38 Comment(3)
Thank you Pointy! That is a very interesting approach to solving this.Mireielle
@Mireielle you're welcome! Like I said, I'm not super-sure this is widely supported; it may be however, since the opener thing is really old. I didn't know that links/forms with "target" would set it until I tried it for your question :-)Ownership
This does seem to be the best way of dealing with this situation. It looks like although window.opener is not part of an official specification, it's supported by all major browsers and is unlikely to go away... developer.mozilla.org/en-US/docs/DOM/window.openerMireielle
A
1

Adding to the accepted answer by @Pointy:

In 2023, at least in Chrome when a window is open through <form target="_blank"> the window.opener is undefined in the new window.

I had to add rel="opener" to the opening form and after that, the window.opener is defined and can be used as advised by the accepted answer.

<form ... target="_blank" rel="opener">
Arne answered 9/10, 2023 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.