Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  116] [ 2]  / answers: 1 / hits: 24668  / 15 Years ago, fri, july 10, 2009, 12:00:00

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.


More From » javascript

 Answers
8

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.


[#99149] Tuesday, July 7, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;