3

Is there way to access a popup window (created using window.open) from a different tab/window if both tabs/windows are in the same domain?

fahmi
  • 591
  • 6
  • 27

1 Answers1

2

I found this article by Daniel Brain about the state of getting window references: https://medium.com/@bluepnume/every-known-way-to-get-references-to-windows-in-javascript-223778bede2d

Short answer is if they are part of the same window group, you probably can access the popup, but if they aren't, there doesn't seem to be a general way to access the popup. I believe a window group is the windows and frames that share a common ancestor (they were created by a common window).

To me, that makes sense, because windows in separate groups may not be in the same process, and that would cause problems if you were to try to synchronously access variables inside same-origin frames.

For an example of accessing popups from the same group, let's say we have a page, opener.html that opens the window:

var popup = window.open('./my_popup.html', 'popup_name');

In our original window, we navigate to another page accessor.html, on the same domain (and window group), we can then access the popup via:

var theSamePopup = window.open('', 'popup_name'); // doesn't really reopen the window

From my limited testing, this seems to work, as long as we use the original window. We can navigate to another website, then navigate back, and that window can access the popup it made. But when I open up a new tab with accessor.html it makes its own popup.

That said, window references aren't the only way to pass information or coordinate information between windows of the same origin. You can use storage events with localStorage or sessionStorage, use service workers, BroadcastChannel, cookies, or a server side solution.

See:

Communication between tabs or windows

http://craig-russell.co.uk/2016/01/29/service-worker-messaging.html

Steve
  • 10,435
  • 15
  • 21
  • Thanks for your answer. I was wondering if there is a way to reuse the existing popup but as you said it works under certain condition. – fahmi May 24 '18 at 22:44
  • Glad that I could provide some clarification. Depending on your use case, you might be able to reuse a popup by setting its content based on window messages, e.g. from a `BroadcastChannel`. – Steve May 25 '18 at 01:08
  • Unfortunately the two popups might not belong to the same 'window group' but same origin. Also, I need to support IE11 and BroadcastChannel is not available. – fahmi May 26 '18 at 12:07