javascript refresh popup from another popup - javascript

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/

Related

Lost reference created window after surfing

I have creating a new browser window win by #new-window-idclick. And I have events system with that window. Closing win.closed for example. Everything is work until I go to links inside the created window.
$('#new-window-id').on('click', function(){
var finalUrl = $(this).attr('href');
var win = window.open(
finalUrl,
'fullWindowMode');
console.log('created '+ win.name);
var timer = setInterval(function() {
if(win.closed) {
console.log('closed '+ finalUrl);
clearInterval(timer);
document.getElementById('#iframeID').contentWindow.location.reload();
}
}, 1000);
});
So I have iframe on main page. I want to open it in a new window and edit a content there. Then after closing created window, my main page iframe should be updated.
Steps:
Click the link #new-window-id/ //window is opened.
Close window. win.closed is work!
When doesn't work
Steps:
Click the link #new-window-id/ //window is opened.
In the window edit blog post. (url was changed)
Close window. event win.closed doesn't work!
But it breaks if I surfing inside created window. Exists way to keep this connection?
I did the following:
1. Created a window let w = window.open('http://ya.ru')
2. Navigated in that window (within the same site)
3. Checked w.closed in the main window (false)
4. Then closed the popup and checked again (true).
Looks like w.closed works even after navigation.
But if you navigate to another origin in the new window, then location.reload() will become inaccessible from the main window.

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/

How to return focus on parent window from a pop window while loading first time

I am working on a website. Where while loading website first time opening index page with one pop window. First my problem is I want to minimize this pop window automatically while loading first time and focus return to website without any click in website.
Here is my code in index page I write this function:
myFunction();
function myFunction() {
//var myWindow = window.open("music.php", "", "width=320,height=60");
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
//myWindow.moveTo(0, 0);
}
And pop window having page extension music.php which should be minimize automatically and focus return to index page without any click on website.
I have already try with set the focus of a popup window to website everytime , but it was also not working.
Well, The window object has an opener property as well.
When we open the browser, that property is null, and when we open a popup , it points to the window the popup has been opened through.
I think something like this should suffice.
myFunction();
function myFunction() {
var myWindow = window.open("music.php", "", "directories=0,titlebar=0,toolbar=0,location=0,status=0,menubar=0,scrollbars=no,resizable=no,left=-100000000, top=100000, width=320, height=1, display=none","visibility=hidden");
myWindow.opener.focus() // can also do window.focus()
}
The execution of JS should not stop, but if it does, you have to add the focus code to your child page/popup page.(Depending on varying browsers)
like
popupHTMLJS.js
focusParent();
function focusParent(){
parent = window.opener;
parent.focus()
}

Obtain reference to existing browser window

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) ...
}

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.

Categories