Page 1 of 1

Javascript Close Function

Posted: Sun Oct 19, 2003 2:08 am
by desmondlk
Hi All,

I am using the below file (Test.php) as my first page which has a link and navigate to Test2.php when click on the link.

[Test.php]

<SCRIPT LANGUAGE="JavaScript">
function openBar(){
window2=open('showprog.php', 'Show_Progress', 'location=no,scrollbars=no,menubar=no,toolbar=no,status=no,resizable=no,top=240,left=250,height=30,width=300');
}
</SCRIPT>

<a href='Test2.php' onClick="javascript:openBar();"> Testing</strong></a>

<!--end-->

When I click on the link, it leads me to Test2.php and meanwhile it popup a new window called 'window2'.

In Test2.php, a code is used to close the 'window2' during the page is loading (Please refer to below). However, it shows an error message stated that 'window2 is undefined'. May I know what is the error in the code?

[Test2.php]

<html>
<head>
</head>
<body onLoad="javascript:window2.close();">
</body>
</html>

<!--end-->

Please help.

Thanks in advance.

Regards,
Desmond.

Posted: Sun Oct 19, 2003 6:38 am
by cybaf
This problem is a bit tricky and the new parent window does not have access to the child, because of security reasons.

However, there is a "nice" workaround to create the effect I'm suspecting you want.

test.html:

Code: Select all

<html><head><script language="javascript">
function bar() &#123;
	myWindow = window.open('popup.php','myWindow');
&#125;

</script>
</head>
<body><a href="test2.html" onClick="bar()">test</a>
</body></html>
test2.html:

Code: Select all

<html><head><script language="javascript">
function foo() &#123;
	myWindow = window.open('closeme.html','myWindow');
&#125;
</script>
</head>
<body onLoad="foo()">test2
</body></html>
closeme.html:

Code: Select all

<html><head>
</head>
<body onLoad="self.close();">test closing
</body></html>
This works since you set the name of the opened window to myWindow in this case. Then when you open a "new" window with the same name the url in the old popup will change to the new one, and in this case the new url only closes itself. Which was what you wanted. :)

//cybaf