Obtain reference to existing browser window - javascript

I'm 99% sure that the answer to my question will be no but just in case...
Use case:
Webpage A opens a new window B with JavaScript (same domain)
The user refreshes A or navigates away and then returns
The user clicks on a link in page A which should close B
This works fine by holding on to the window reference as long as the user does not refresh and navigates away from A (step 2).
Is there any (exotic or not) way to re-obtain the reference to the previously opened window(s) when returning to A?

Yes
function openWin() {
w = window.open("","windowname"); // use the window name you gave it when you opened it
if (w && !w.closed) ...
}

Related

How to check the existence of Children Window

I have a JSP page A. And on page A, there are some links when click on them, page B or C will be popped up.
Now I have a javascript to run in page A. How can this javascript determine whether there are existing popup page B or C?
First of all you need to ensure that you have references to your opened windows available with you. It is currently not possible to get a list of already open window. If you are unsure you can refer to this answer that suggests a decent way of doing this.
Now, using the window object of the child window, you can check if it is null or not in order to know whether it is open or not:
var myWindow = window.open("https://www.google.com", "MsgWindow", "width=200,height=100");
function IsWindowClosed (win) {
return win.window == null;
}
console.log("Window is closed: ", IsWindowClosed(myWindow));
Here's a working fiddle: https://jsfiddle.net/6vhgpkmm/1/

Javascript Popup Window as a singleton

I have a page A which opens a javascript window as myWin = window.open(..). Now in another page B in the same domain, when user clicks on a link, I want to check if myWin is available, if yes then bring that window in front, else do a new myWin.
The problem I have is the window.js file in both pages and window.js contains the line
var myWin = null;
Within Page A scope, my logic works and brings window to front. Within Page B it works as well. Howver when I open window in Page A, click on link in Page B, it fails.
It is as if myWin is reset to null in scope of Page B. How can I overcome this problem? Pointers??/
The popup window is created by page A and only page A contains a reference to that window. When you open page B, there is no way to pass page A's reference to the popup over to page B. This cannot be done.
You want to call window.open with an empty url, e.g.
var newWindow = window.open('', title, dimension); // title needs to be a constant
if (newWindow.location.href && newWindow.location.href!='about:blank') {
// Window was already open and points to something
newWindow.focus();
} else {
// new window make it point to something
newWindow.location.href = 'http://yoursite';
}
This assumes that the popup is opening a window within the same site as the calling page, since otherwise you can encounter various permissions issues.

javascript refresh popup from another popup

Window A opens window B
Window A opens window C
On Window C (after user action) I need Window B refreshed.
some more explanation:
yes. Window A is the main calendar. window B is opened manually and is smaller and shows stats about the calendar (window A)
When user clicks on a calendar event in window A then Window C opens. And when the user changes info on Window C then Window B (stats) needs to update.
First of all I have to warn you that what you are trying to achieve is not a good practice.
Many browsers are opening new tabs instead of new windows, which can influence the availability of the window.opener . Although this depends heavily on the browser settings.
So first of all you should test to see that your code is really opening new windows, not new tabs.
Now, in a.html you should have this:
var windowB=window.open("b.html");
var windowC=window.open("c.html");
In c.html you should have this:
function openNewPage() {
window.opener.windowB.location.reload(true);
}
and call the openNewPage() function on user interaction.
You need to set references to each other the child windows to each other. Here's a simple demo.
var b= window.open('');
var c= window.open('');
b.test=function() {
alert('Function in window B');
};
c.test=function() {
b.test();
return false;
};
$(b.document).find('body').append('<div>Window B. Click here</div>');
$(c.document).find('body').append('<div>Window C. Click here</div>');
http://jsfiddle.net/9ZCX3/25/

window.close(); not working when page changed or refreshed

I have this little function to open/close a popup player:
function popuponclick(popup)
{
my_window = window.open("folder/player-itself.htm", popup, "width=350,height=150");
}
function closepopup()
{
my_window.close();
}
I call the functions from HTML anchors that are on each page of the site (idea is to have the player stopped/started whenever you want)...now...
it works well until i change the page, or refresh the existing one - and from then the window can't be closed anymore. Any idea where i'm wrong? Tested in FF and IE8, same behavior.
Thanks for your help.
When you reload the original window (or tab), everything about the old one is gone, blasted into the digital void, never to be seen or heard from again. The bits literally disintegrate into nothingness.
Thus, the "my_window" reference you so lovingly saved when the second window was opened is gone for good, and the "my_window" variable in the newly-loaded window contains nothing. It's name is but a mockery of the variable in the now-dead page.
The only way to deal with this situation is for the popup window to periodically check back via "window.opener" to see if its parent page has been rudely replaced by some interloper. If that happens (and the new page is from the same domain), then the popup page can restore the reference to itself in the new page's "my_window" variable.
edit — OK here's a sample. You'd put something like this in the popup page, not the launching pages:
<script>
var checkParent = setInterval(function() {
try {
if (window.opener && ('my_window' in window.opener))
window.opener.my_window = window;
}
catch (_) {
// clear the timer, since we probably won't be able to fix it now
clearInterval(checkParent);
}
}, 100);
</script>
That's probably pretty close.

Find window previously opened by window.open

We've got the following situation, running from a single domain:
Page A uses window.open() to open a named window (a popup player). window.open() gives page A a reference to the window.
User now reloads page A. The reference to the named window is lost. Using window.open() to "find" the window has the unfortunate side effect of reloading it (undesirable). Is there any other way to get a reference to this window?
Try this:
var playerUrl = 'http://my.player...';
var popupPlayer= window.open('', 'popupPlayer', 'width=150,height=100') ;
if(popupPlayer.location.href == 'about:blank' ){
popupPlayer.location = playerUrl ;
}
popupPlayer.focus();
It will open a blank window with a unique name. Since the url is blank, the content of the window will not be reloaded.
AFAIK, no there isn't..
A kind-of-dirty-but-i-guess-it-will-work hack would be to periodically reset the reference on the parent window from within the popup using window.opener, with something like this code:
setInterval(function() {
if(window.opener) {
window.opener.document.myPopupWindow = window
}
}, 100)
In the parent window, you'll be able to access document.myPopupWindow, even after a reload (well, 100ms after the reload). This should work cross browser.
Actually what you did is destroy the parent (page A) of the created window (Popup), so it has no more reference to the original parent therefore you can't get a direct reference.
The only solution I can think of is using a browser that offers you added javascript capability to cycle through active windows (tabs) and find one that has a special property (ie: your reloaded page A) that gets recognized by the popup.
Unfortunately I guess only firefox has some added capability or extension that gives you this flexibility. (it is also a security risk though)
This should work. Add this code in the popup:
function updateOpener() {
if (window.opener)
window.opener.document.myPopupWindow = window;
else
setTimeout(updateOpener, 100);
}
updateOpener();
And this in onload of the parent window. To make sure myPopupWindow have been set wait 100 ms before accessing it.
setTimeout(function() {
if (document.myPopupWindow)
document.myPopupWindow.focus();
}, 100);
If all the windows share a common Url origin you can register a ServiceWorker and then access all windows from the ServiceWorker: https://developer.mozilla.org/en-US/docs/Web/API/Clients
AFAIK You won't be able to pass a reference to other windows from WorkerService to your window but you can establish communications with the ServiceWorker via
https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage
It Might help someone, If you opened an child tab and after refreshing the parent tab, you still want to focus on that child tab instead of opening new child tab: -
const chatPopup = window.open('', 'chatPopup');
if (chatPopup.location.href === 'about:blank' || !chatPopup.location.href.includes('/chat')) {
this.openNewWindow = window.open('/chat', 'chatPopup');}

Categories