ColdFusion, WordPress, Flash & other web things


Cross-window Javascript communication 2.0

Back in April 2005, I wrote about a JavaScript system to regain control of an orphaned popup window. A couple of days ago, Robert Emmery left a comment stating that he had a simpler solution that didn’t require the somewhat clumsy notifier mechanism. Of course, at first, I didn’t believe him. Surely, nobody can solve a problem that I thought was insolvable! ;-)

Anyway, I challenged Rob to produce an example that reproduced exactly what mine did and he did so I declare my solution officially obsolete. Here’s Rob’s solution:

In the main window:

var popupWin = null;

function openPopup() {
	var url = "popup.htm";
	popupWin = open( "", "popupWin", "width=500,height=400" );
	if( !popupWin || popupWin.closed || !popupWin.doSomething ) {
		popupWin = window.open( url, "popupWin", "width=500,height=400" );
	} else popupWin.focus();
}

function doSomething() {
	 openPopup();
	 popupWin.doSomething();
}

In the popup:

self.focus();

function doSomething() {
	alert("I'm doing something");
}

As you can see it’s much cleaner. Thanks Rob!

Rob is the founder of SpaceCatch Inc, a social networking service that will launch some time in January.

You can read about the original solution and it’s problem here.

Download an example

20 comments

Pages: [2] 1 » Show All

  1. #20: ??????? ??????? ?????????? Says:

    ??????? +10

  2. #19: Gio Says:

    hi guys,
    i find this solution very usefull, but doesn’t work in Safari for windows.

    does somebody know a solution?

    thanks a lot

  3. #18: Will Says:

    This solution has a limitation.

    It does not work if popupWin goes to a URL with a different domain to that of the main window.

  4. #17: Tom Says:

    The problem is you call your javascript function too early;-) You have to wait the popup page be loaded before call a javascript function.

  5. #16: Adam Says:

    Brian-

    It sounds like it might be a security setting in IE.

  6. #15: Brian Says:

    Re the above - I get the error in IE 6 but not Firefox.

  7. #14: Brian Says:

    Anyone have security problems with this? Once I’ve launched the popup, if I call the openPopup() again, I get a permission denied trying to access popupWin.doSomething in the if statement.

    Weirdly, if I reload the page that is calling the openPopup() function and try again, it works without complaint.

    This only happens when the client is on a different domain than the server. I can also eliminate the error by loading another page from the same domain before going to the page that calls openPopup().

    Any ideas?

  8. #13: Adam Says:

    Johann-

    The problem with that is you don’t know when the page is finished loading. That depends on browser speed and whatever your computer happens to be doing at the time. Clearly, if you’re running either an I/O or memory intensive program your browser is going to be sluggish because your computer is going to be sluggish so you can’t say “Oh, I’ll just wait 5 seconds and then assume the page has finished loading and the function is there.”

    This is the point of having the onload event handler. It essentially guarantees that the page has finished loading and everything that’s exposed is available.

  9. #12: Johann Says:

    Shouldn’t you rather set an interval after opening the window that checks whether the opened window has the function you’re looking for?

  10. #11: Nicholas Says:

    I go one weird problem. As usual its a browser issue. My scrips works perfectly in IE and Opera but not in Firefox. I don’t know why. Here is the problem.
    I was able to open pop up window
    Select Image

    function showUpload(obj)
    {
    window.open(obj.href, “imageUploadWindow”, “width=400,height=630,scrollbars=no,resizable=no”);
    }
    ***************
    Now when I try to pass value from popup window to main window like this

    function updateImages()
    {
    var imageName = document.getElementById(”photolist”).value;
    eval(”self.opener.document.form.image.value=’”+imageName+”‘”);
    }

    —————————————-
    It does not work in Firefox. But works perfectly in IE and Opera.
    Any help or suggestion will get great appreciation.
    Thank you

Pages: [2] 1 » Show All