Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-includes/theme.php on line 623

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-content/plugins/paged-comments/paged-comments/paged-comments.php on line 159

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-content/plugins/paged-comments/paged-comments/paged-comments.php on line 380

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-content/plugins/paged-comments/paged-comments/paged-comments.php on line 382

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-content/plugins/paged-comments/paged-comments/paged-comments.php on line 386

Deprecated: Assigning the return value of new by reference is deprecated in /home/onepixel/public_html/wp-content/plugins/paged-comments/paged-comments/paged-comments.php on line 481
1 Pixel Out » Blog Archive » Cross-window Javascript communication 2.0

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

  1. #1: michi Says:

    Hello,
    nice solution, but I have problems with it. Copied js code exactly from this page, tried and it is not working ;( the popup opens, but doSomething function is not fired. in firefox I get following error: Error: popupWin.doSomething is not a function. but it looks on some strange problem, because when I tried it more times (only by refreshing the page and clicking on a button), it worked sometimes. do anyone has any solution?

  2. #2: Martin Says:

    michi: It works here. Send me your example so I can have a look. My email is martin at this domain

  3. #3: Markus Ernst Says:

    Nice one! I spent hours searching for some kind of getWindowByName() functionality, read tons of tutorials that all go obsolete as soon as the opener window is reloaded. Your solution is really very helpful!

  4. #4: DK Says:

    This works in IE 6 but it does not work in Firefox 2.0.0.3. I get the error

    popupWin.doSomething is not a function

  5. #5: Eric Says:

    This is just what I was looking for. It works great in Mac OS X with Safari, Firefox, Opera, Camino, iCab, Netscape and even old Firebird. With Explorer (5.23), surprisingly, the pop-up window never opens.

  6. #6: Joachim Lous Says:

    I stumbled on almost the exact same solution. I didn’t think an empty url would leave the popup alone,though, so I used an empty javascript: url in stead. But then there’s the problem that led me here:

    In my case, I don’t want to open the popup if it isn’t already there,
    just send a command if it is.

    The ‘open’ call will always return a window, possibly to an empty new
    one. I suppose I could close it again immediately if the URL or
    contents reveal a dummy, but it’s not very elegant; this operation
    will occur quite frequently in my app.

    So: is it possible to check for the pre-existence of a named window?
    (without using timers in the popup, of course. The popup can be
    cooperative i initialising, if that will help).

  7. #7: Adam Gordon Says:

    Actually, for complex or slow webpages/sites, the above example won’t work because you have a race condition between the second page loading and the first page executing the javascript function call on the second page. The function doSomething() doesn’t exist until the browser parses that code and says it exists. This is some people above are getting the error.

    One solution is to put a sleep timer in between the window open call and the call to doSomething(), however again, for complex or slow webpages or sites, you have no idea how long this delay needs to be and Firefox kindly warns the user if a JavaScript function executes for too long.

    I believe the right way would be to override the window’s onload event handler to ensure that everything exists before executing any function call. But what if you already have an onload function defined? How can I get more than one function to execute at page load time without having a massive function or a function that calls a bunch of other functions?

    I’m glad you asked. The solution is to have a callback manager for the onload event handler. You can find the code here: http://simonwillison.net/2004/May/26/addLoadEvent/

    Do a search on “Andreas Karlsson” to find his callback manager code. Basically it maintains an array of functions to execute. This way, you can define your functions via “var foo = function { … } and then call “window.onload.addCallback(foo); to register your function. This way, you are guaranteed that the page has finished loading and you don’t need any sleep scripts.

    –adam

  8. #8: Adam Gordon Says:

    Whoops, that should be “var foo = function() { … }” in the last paragraph. Sorry. Also, missing the word “why” from the last sentence in the first paragraph.

    –adam

  9. #9: Kylir Horton Says:

    Thank you for the excellent article. I’ve used the techniques here to create a fairly complex object that manages some pop up windows in a web application. It is pretty slick and this was a key component in it.

    Also, thanks Adam for your post about adding onload events to new pop ups. I used Simon’s functions to create some pretty sweet communication between windows. Thanks again everyone!

  10. #10: mika Says:

    Thank you a million time for this I’ve been looking to do this since quite a long and you perfectly solve my problem.
    Hugs

    mika

  11. #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

  12. #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?

  13. #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.

  14. #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?

  15. #15: Brian Says:

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

  16. #16: Adam Says:

    Brian-

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

  17. #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.

  18. #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.

  19. #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

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

    ??????? +10