How to call javascript from different window? - javascript

I am working with IE9. Want to open a popup window from a page, and from the popup, how to call the javascript code in the page (not the popup window)?
There will not be a cross domain issue as I am going to load a page from the same domain of the page into the popup window.

Like Cory said, use window.opener to get the parent window's window. Look at this:
http://jsfiddle.net/Dmnqk/
function openPopup() {
var w = window.open("", "");
w.document.open();
w.document.write("<script type='text/javascript'>window.opener.parentWindowFunction();<\/script>");
w.document.close();
}
function parentWindowFunction() {
alert("called");
}
Of course, my use of document.open/write/close is simply to create my own page that calls the parent window's function (technically, it should really have <html><head></head><body>SCRIPT HERE</body></html>. All your popup's page would need is the call to window.opener.parentWindowFunction.

You can use window.opener to access functions in the global (window) namespace of the page that opened the popup:
Opener
function functionFromOpener() {
return "wharrgarbl";
}
Popup
alert(window.opener.functionFromOpener());
Obligatory jsFiddle example: http://jsfiddle.net/9yLu2/1/
That said, I think it's a better idea to use in-page DHTML dialogs. (Or possibly it's that browser windows that aren't resizeable / don't have address bars bug me that much.)

Related

How to control opener page which is opened by window.open method

I have a page in which i m calling another popup by window.open method. The only thing how can i change a label in opener page from popup page while the popup page is still alive ie which is not closed yet
It's better to let the opener window take care of changing values by exposing a small API to the popup window.
I've outlined it here: javascript - pass selected value from popup window to parent window input box
It should be like this:
window.opener.document.getElementById('label1').value = "the new value";
<script>
function myFunction() {
var additionalWindow = window.open("/additional");
// Write on the additional window
additionalWindow.document.write('written from separate window');
// Call a function on the additional window
additionalWindow.someFunction();
}
</script>
Here's Mozilla's documentation on window.open().

Detect browser window in javascript

I'm a newbie in javascript and I've to do the following operation: I have an asp .net application which load in a new window browser another content.
Through javascript, I need to refresh the popup which i opened before, but I don't know how I can find that new window.
I tried a simple window.location.reload(true); but this will cause a refresh only of the main browser window which launched the popup. Instead my intent is to refresh only the popup window.
When you open the window, give it a name (or store the return value of the window). Then, instead of window.location.reload, call name.location.reload where name is the name you gave the window.
If you open the window with window.open(), grab its return value. That's a reference to the window object of the popup. From there you can call location.reload(). For example, this will open a window and then reload it again:
var popup = window.open('/somepage');
popup.location.reload(true);
Store a reference to your popup window and then instead of calling the location.reload() function on your main window, call it on the popup window, like this:
var myNewWindow = window.open(...);
myNewWindow.location.reload(true);
Are you creating the new window using Javascript?
if you are using window.open(), that function returns a reference to the new window, So you can do something like this:
var newWin = window.open();
newWin.location.reload(true);

Access a window by window name

If I open a window using
window.open('myurl.html', 'windowname', 'width=100,height=100');
How do I refer to the new window (from the same page that opened it) using 'windowname'? This question is specifically about this. I'm aware that I could save a reference to the handle by using "var mywin = window.open(...)" but I don't care about that in this situation.
Thanks, - Dave
In firefox (might work in other browsers too, but now it's not my concern) I was able to reference one window accross multiple page loads with
var w = window.open("", "nameofwindow");
This opens new window if it doesn't exist and return reference to existing window if it does exist without changing contents of the window.
With jQuery I was then able to append new content, to make quick collection of interresting links like this
$('body', w.document).append(link_tag);
If you didn't save a reference to the window then there is no way to restore it. However, if that window is still open and if the page loaded there belongs to the same domain as your page, you can run JavaScript code in it:
window.open("javascript:doSomething()", "windowname");
Whether that's sufficient in your scenario depends on what you are trying to achieve.
Petr is correct:
var w = window.open("", "nameofwindow");
works in all browsers, I am using it to retrieve the reference to the window object previously opened by a different page. The only problem is the initial opening of the page, if the popup does not exist, you will get a new window with a blank page.
I tried invoking a Javascript function inside the context of the other document in order to check whether I opened a new window or retrieved the already active page. If the check fails, I just invoke window.open again to actually load my popup content:
var w = window.open("http://mydomain.com/myPopup", "nameofwindow");
Hope that helps.
It is not possible. The windowName is just to be used in target="..." of links/forms or to use the same name again in another window.open call to open a new url in that window.
Try open that window with the name, but URL is '' again, to check if it's a blank window or not. If it's in open, then you will get the window; if not, a new window open, and you need close it.
Add the children in localStorage will help to prevent to open the new blank window.
Please check my code in https://github.com/goldentom66/ParentChildWindow
Sorry I am posting late, but if you still have the other window open, and they are on the same domain, you can run, on the first window:
function getReference(w) {
console.log('Hello from', w);
}
And on the second window:
window.opener.getReference(window);
afaik there's no way like windows['windowname'].
The 'windowname' assigned in window.open() can be addressed as a target in <a target="windowname" [...] >

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

Changing the address of the opener window in IE8

From a child popup window (opened using window.open), I am trying to change the URL of the window that opened me, e.g.
window.opener.location.href = 'http://www.google.com';
In all browsers this works wonderfully, except for IE8 (and I am somewhat sure it worked in previous IE8. Maybe a security update kills this).
In IE8 what happens is that the line above is treated as a request to open a NEW window with the address and the original opener window stays the same. And, since I am not putting this line inside an onclick event, this is treated as a popup.
So how do I do this in IE8? How do I change the opener location?
It looks like this is just not possible. I got no answer from anybody and all my research indicated that this is just another way MS added to block popups.
try to move action into parent window, like this:
//parent.htm
function changeUrl(url) {
location.href = url;
window.reload();
}
window.open("child.htm");
.....
//child.htm
window.opener.changeUrl(url);
try this:
on the opener window define a function called goto ;-)
function goto(url){ window.location.href = url; }
now from the child window call window.opener.goto(url);

Categories