Click to See Complete Forum and Search --> : window.opener.document


Dave_NI
11-28-2003, 07:29 AM
Hi all.

I have a generic peice of javascript which is run on ever page. Its designed to copy the contents from one form to an identical other form. It works a treat, but the problem is in the code is still run against pages with no forms, which causes a error.

Basically I need to check if the parent window has a document object... but if I reference it, I get the error.

My referencing coed is-

var parentForm = window.opener.document.forms[0];

I have tried to assign
var parentForm = window.opener.document
and check if its null but it still returns " window.opener.document is not an object".

Can I test to see if the parent has a document without it falling on its ass?

Thanks

Gollum
11-28-2003, 07:55 AM
try this...

try
{
if ( window.opener.document )
{
alert('found');
}
}
catch(e)
{
alert('error');
}

gil davis
11-28-2003, 07:58 AM
var parentForm = null;
if (window.opener.document)
{if (window.opener.document.forms[0])
{parentForm = window.opener.document.forms[0];}
}

Dave_NI
11-28-2003, 08:50 AM
Both suggestions I think are valid...

Didn't realise you caould use TC's in JavaScript...

Cheers.

gil davis
11-28-2003, 08:53 AM
Try/Catch is a new addition. It won't work unless the browser supports ECMAScript or JavaScript1.5. My suggestion will work in any version.

Dave_NI
11-28-2003, 09:07 AM
What versions of IE and Netscape would not support it?

gil davis
11-28-2003, 11:14 AM
IE 5.0 and above support it (JScript 5.0).
NS 5 and above support it (JavaScript1.4).

Gollum
11-28-2003, 11:59 AM
In some circumstances accessing window.opener.document will cause problems, like if the parent window is displaying a pdf file - you get an access restriction