tags:

views:

824

answers:

1

I have asked this question before and the problem was half solved in the sense I was helped to find out that Javascript has some tough security in place.

What I learnt: A parent Window opens a child window. The child window redirects to a different domain and gets redirected back. It attempts to fire off a function of the parent window upon closing itself.

window.opener.startLoad();

This leads to a permissions (security) problem and will not work.

(Half) New Problem: How can I get a window to open a child window and when it closes run a function in the parent window?

I need a very efficient way of doing this as this will be happening a lot!

Thank you for any help.

+3  A: 

Try this:

var win = window.open(url, name);
win.onunload = afterChildClose; // afterChildClose() is the function.
win.close(); // afterChildClose() should fire now.

All this code would be located and executed in the parent window's javascript (including afterChildClose()). You are creating the window and assigning the function to the unload event.

Oh, one thing I just thought of. If the child window refreshes or navigates to another url, the onunload event will fire as well. If you only want the event to fire when the window is closed, you should include a check in afterChildClose() on win.closed.

EDIT: Here is the code to two sample pages I put together to try this out.

Parent window:

<html>
<body>
<script type="text/javascript">
    var win = window.open("secondwindow.html", "woo");
    win.onunload =onun; 

    function onun() {
        if(win.location != "about:blank") // This is so that the function 
                                          // doesn't do anything when the 
                                          // window is first opened.
        {
            alert("closed");
        }
    }
</script>
</body>
</html>

child window ("secondwindow.html"):

<html>
<body>
<script type="text/javascript">
    setTimeout('window.close();', 5000);
</script>
</body>
</html>

When I try opening the first document in firefox, it opens the window, the window waits 5 seconds and quits, and the first document shows the alert(), so it is working even when the sub window closes itself.

Sean Nyman
Where does the afterChildClose function reside?
Abs
So are you closing the Child window from the parent window?
Abs
Yes, the `win.close()` is just there to demonstrate what will happen when the child window is closed. The code in my answer is not your full solution, it merely shows the technique that will allow you to do what you need. That technique is to capture the child window object after you open it, and then assign events to that object (such as onunload).
Sean Nyman
Ok, I see. I am having trouble referencing that child window because it opens on an onclick event, maybe I can reference by child window name??
Abs
I solved the previous problem. I am just having difficulty referencing win in the function as it says its undefined. Function is below the above code in its own script so not sure why its not working. Are you sure that function is run on the child window closing and not it just opening as this is what it looks like when I am testing it??
Abs
Ok another update, this is partially working for me. It runs that function when it redirects (half the job) but it doesn't run the function when it the child window closes itself??
Abs
Hmm. I just tried putting together a test page that does this, and it seems to be working. I'll update my answer with the code in a few minutes.
Sean Nyman
Thank you for your code explanation! My code matches yours and the code is working as expected but it never fires that function again upon close. It does so ONCE when it redirects back to me but after that it doesn't?? Maybe this event is only done once, do I need to set it again? I am very grateful you got me this far as I couldn't even get the function to be fired at all before. :)
Abs