最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - obtain reference to _blank target window on form submit - Stack Overflow

matteradmin4PV0评论

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.

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.

Share Improve this question edited Sep 3, 2013 at 17:45 Sergio 28.8k11 gold badges89 silver badges132 bronze badges asked Nov 26, 2012 at 17:15 jtrickjtrick 1,36918 silver badges23 bronze badges 1
  • 1 Why would someone vote this question down? At least a ment 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. – jtrick Commented Nov 26, 2012 at 17:34
Add a ment  | 

3 Answers 3

Reset to default 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
}

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');

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">
Post a comment

comment list (0)

  1. No comments so far