Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Parent Window 1

Status
Not open for further replies.

dodgyone

Technical User
Jan 26, 2001
431
GB
I would like to be able to open a child window and then check when returning the value to the parent that it is still open. My code used is as follows:

=============
[PARENT PAGE]
=============
<script language=&quot;JavaScript&quot;>
<!--
function go(){
window.open(&quot; &quot;&quot;,&quot;toolbar=1,directories=0,status=1,menubar=0,scrollbars=yes,resizeable=no,width=788,height=500&quot;);
}
// End -->
</script>

<input type=&quot;submit&quot; name=&quot;Submit3&quot; value=&quot;Image Library Database&quot; onclick=go()>

============
[CHILD PAGE]
============
<SCRIPT LANGUAGE=JAVASCRIPT TYPE=&quot;TEXT/JAVASCRIPT&quot;>
<!--- Hide script from old browsers

<!-- used in [index_view_buttons.inc] to send data to aplaws -->
function process() {
$Transfer = opener.document.form1.data.value = '<? echo $filename; ?>';

if ($Transfer) {
self.close();
}
else {
alert(&quot;The process has been cancelled because the destination window has been closed...&quot;);
}
}

// End hiding script from old browsers -->
</SCRIPT>

<a href=&quot;javascript:eek:nclick=process()&quot;>

There are other things to consider. In the child window I may have to navigate through a few pages first (it is not a static, one page window) before sending the data back to the parent. The code has been placed on the send page where the user navigates to.

Another problem. The parent window may be closed, moved on or back from the original page (after opening the child) where the procedure commenced. It is important to be using exactly the same page as was used to start the procedure and not just to re-open the url and paste the data into the relevant text box. If it is not on exactly the same page then just produce an alert message and do nothing with the data.

Many thanks for any suggestions or ideas...
 
in the child window, you should be able to do something like this:
[tt]
<SCRIPT LANGUAGE=JAVASCRIPT TYPE=&quot;TEXT/JAVASCRIPT&quot;>
<!--- Hide script from old browsers

<!-- used in [index_view_buttons.inc] to send data to aplaws -->
function process() {
var parentWin = opener;

if (parentWin &&
parentWin.document &&
parentWin.document.form1 &&
parentWin.document.form1.data)
$Transfer = parentWin.document.form1.data.value = '<? echo $filename; ?>';

if ($Transfer) {
self.close();
}
else {
alert(&quot;The process has been cancelled because the destination window has been closed...&quot;);
}
}

// End hiding script from old browsers -->
</SCRIPT>
[/tt]

if the parent window has been navigated from the page that opened the child, the reference between the parent/child will most likely be destroyed and the script above should throw the alert().

=========================================================
while (!succeed) try();
-jeff
 
Thanks for the help but I still seem to be getting error messages with the first section of the if statement:

if (parentWin &&
parentWin.document &&
parentWin.document.form1 &&
parentWin.document.form1.data)

I used MS Developmenmt Environment and it came up with the error that &quot;Access in denied&quot;.

Has anyone any ideas? Thanks...
 
if the parent window and child window are not on the same domain, you will not be able to access one from the other.

e.g. parent: child: =========================================================
while (!succeed) try();
-jeff
 
I thought I was on the same domain. I have the files used in exactly the same directory so they aren't pointing to other domains.

The PHP scripting does point to a MySQL database but that's also on the same drive as well.

I'm pretty stuck now ;o(

Thanks for the help though jemminger...
 
Sorry, let me clarify things a little better.

I can send the data to the parent form if it's leff on in the background. The error only comes up if the parent page has been closed or changed. That's when the &quot;Access is denied error&quot; appears.

Surely the data send wouldn't work as well if it was on different domains?
 
hmmm...seems to work ok for me with a test page...

try putting
var parentWin = opener;

outside of the function &quot;process()&quot; to make it global. =========================================================
while (!succeed) try();
-jeff
 
Nope, tried global variables. I haven't a clue what could be wrong but it doesn't like the starting if code.

Any suggestions out there?
 
I'm still stuck about how to error trap passing the variable from child back to parent if the parent browser page has been moved or closed.

The error trapping works fine if I just open the child browser window without using the parent. If you try to send the variable to the parent then the error trapping works straight away.

Has anyone got any more suggestions? Thanks...
 
one more idea:
[tt]
<SCRIPT LANGUAGE=JAVASCRIPT TYPE=&quot;TEXT/JAVASCRIPT&quot;>
<!--- Hide script from old browsers

<!-- used in [index_view_buttons.inc] to send data to aplaws -->
function process() {
var parentWin = opener;

try {
$Transfer = parentWin.document.form1.data.value = '<? echo $filename; ?>';

if ($Transfer) {
self.close();
}
}
catch(e) {
alert(&quot;The process has been cancelled because the destination window has been closed...&quot;);
}
}

// End hiding script from old browsers -->
</SCRIPT>
[/tt]
=========================================================
while (!succeed) try();
-jeff
 
Thanks a million jemminger that solved the problem. A well deserved star for all of your hard work.

I'm off now to try and understand what it is you did with the code...
 
all i did was wrap it in a try...catch construct that's been available since js1.3 or so.

here's the basic form:
[tt]
try {
// try to execute some code here
} catch (e) {
// (optional) code to execute if above code fails
}
[/tt]

&quot;e&quot; is a mandatory argument to catch(), and is the Error object passed when the try code fails. it has four properties you can use if desired: name, message, number and description.

for example:
[tt]
try {
alert(x);
}
catch (e) {
for (p in e) alert(p + ': ' + e[p]);
}
[/tt]

the code within the catch() block is optional...you can leave it blank if you simply wish to ignore errors.

=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top