Cross-window scripting

The opener crashes Explorer 3 on Mac and may crash Opera 4.

Hotjava 3 seems to regard the entire window as opener, so that it loads the pages in the complete window, destroying the frameset and thus the opener.

Explorer 3 on Windows has trouble with this script: sometimes you need to click on a link a few times before it loads into the popup.

This script uses focus which is not supported by all browsers.

WebTV puts popups below the actual page in its one screen. You cannot reach the opening window (opener).

If you're using popups, inevitably the time comes when you want to call scripts in another window. This page explains the basics of cross-window JavaScripting.

First the code to access the popup from the opening page, then the code to access the opening page from the popup. Then a short note on closing the opening page.

Opening page -> popup

We extend our earlier popup script a bit to provide for multiple links to the same popup.

var newwindow = '';

function popitup(url)
{
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow=window.open(url,'name','height=200,width=150');
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

If the script is called, we first need to check if the popup is already opened.
This construction is entirely for the benefit of Explorer . If the popup is already open and you open it again with a different URL, Netscape simply loads the new page into the popup, but some versions of Explorer 4 and 5 give nasty errors. Using this method, we avoid these errors.

First of all, define a variable newwindow and make it empty. This variable is either empty or contains the popup window. If we don't define this variable before, we get errors in our main script because the very first time we call the script, newwindow is not defined.

var newwindow = '';

Now we check if the popup is already open. First of all we check if the window is not closed (if (!newwindow.closed). We must check this first, or Explorer 5 on Mac will give error messages.

When the window has never yet been opened, newwindow.closed is undefined (so !newwindow.closed is true, just as if the window were open). Therefore we also check whether newwindow has a location property.

function popitup(url)
{
	if (!newwindow.closed && newwindow.location)
	{

If this is the case, the popup is open and we just have to put the new page into it.

		newwindow.location.href = url;
	}

If the popup doesn't exist or has been closed, we open the new window as described before.

	else
	{
		newwindow=window.open(url,'name','height=200,width=150');
	}

Then a trick for the benefit of Netscape 2. It doesn't natively support the opener (see below). So if the newwindow doesn't have a property opener we make it and set it to self, which means the opening window (in the example: this frame).

		if (!newwindow.opener) newwindow.opener = self;

	}

Finally, we put the focus on the popup if the browser supports the focus method and return false for accessibility reasons.

	if (window.focus) {newwindow.focus()}
	return false;
}

Test the script:
Load the normal popup page you already saw on the previous page.
Load the advanced popup page that can influence the opening window!

(For some silly reason, Explorer 3 may require you to click the link twice if the popup is already open).

Popup -> opening page

Now we've learned to influence a popup from the opening page. The second bit is more interesting: influencing the opening page from the popup. Please load the advanced popup page and try the links in it to see an example.

This uses the JavaScript variable opener. From the popup window, opener accesses the window or frame that has opened the window.

This is the script in the popup:

function to_old_win(url)
{
	opener.location.href = url;
}

and we call it like:

Load the <a href="javascript:to_old_win('../home.html')">homepage</A>
into the main window.

As you see, the focus remains on the popup, which is the way we want it.

If you use frames in the popup itself and want to change the main screen from one of the framed pages in the popup, you should say

top.opener.location.href = url

After all, the page on which this script is is not the one that was opened. Instead, the frameset, always to be reached by top, is the one that was opened, so we want to influence the opener of the top: top.opener.

Closing the opener

This trick is not necessary in Mozilla, Explorer on Mac, Opera and OmniWeb.

This trick does not work in Netscape 4 and lower, Safari, iCab and Explorer 5.0 Windows.

Discovered by Dan Costea.

Occasionally a popup is meant to replace the opening page. What we'd like to do is open a popup to give a window exact dimensions and/or an exact position on the screen and load the starting page of the site in it. Then we want to close the useless opening window.

Theoretically

opener.close()

should be the code from the popup: close the window that has opened this popup.

However, in some browsers it is not allowed to automatically close windows that have not been opened by JavaScript. The line above works fine in Explorer on Mac, Mozilla, Opera and OmniWeb, but not in Explorer on Windows, Netscape 4 and lower and iCab. In these browsers the user is asked to confirm the closing of the window. As to Safari, it does absolutely nothing.

Rather to my surprise it's very easy to get around this confirm box in Explorer 5.5 and 6 on Windows. Explorer merely looks if the page has an opener. If it doesn't the window has been opened by the user and may not be closed without a confirm. So what we need to do is trick Explorer into thinking the opening page has an opener:

opener.opener = top; // or whatever, as long as opener.opener has a value;
opener.close()

This trick doesn't work in Netscape 4 and lower and iCab, these browsers have more sophisticated ways to determine whether a window has been opened by JavaScript.

Try it. This link opens a popup from which you can try to close this window with and without the trick. The popup uses opener.top.opener because this page is in a frameset.
(Interestingly Explorer considers a window opened by target="_blank" as a window opened by JavaScript: the trick is not necessary to close it. Therefore I cannot use it in my example.)