Close Child Window via Parent using setTimeout

J

John Smith

I would like to close a child window I opened from the parent using
setTimout. I have the function below, but it always returns the error
"sendWindow is undefined".

I would appreciate any pointers.


function sendWindow()
{
var sendWin =
window.open('about:blank','sendwindow','width=100,height=100');
setTimeout('sendWin.close()',200);

}

Thank you
 
D

Dietmar Meier

John said:
function sendWindow()
{
var sendWin =
window.open('about:blank','sendwindow','width=100,height=100');
setTimeout('sendWin.close()',200);

}

Statements executed by setTimeout() run in the global execution
context, your variable sendWin is defined in your local function
context only (by using "var" inside the function).

Either you have to make sendWin global ...

var sendWin;
function sendWindow() {
sendWin = window.open(...);
setTimeout("if (sendWin && !senWin.closed) sendWin.close()", 200);
}

.... or you may use a closure, what means keeping sendWin
accessible in an nested function (JS1.2+) ...

function sendWindow() {
var sendWin = window.open(...);
setTimeout(function() {
if (sendWin && !senWin.closed) sendWin.close();
}, 200);
}

ciao, dhgm
 
J

John Smith

On Sat, 2 Apr 2005 14:47:36 +0200, "Dietmar Meier"

:John Smith wrote:
:
:> function sendWindow()
:> {
:> var sendWin =
:> window.open('about:blank','sendwindow','width=100,height=100');
:> setTimeout('sendWin.close()',200);
:>
:> }
:
:Statements executed by setTimeout() run in the global execution
:context, your variable sendWin is defined in your local function
:context only (by using "var" inside the function).
:
:Either you have to make sendWin global ...
:
:var sendWin;
:function sendWindow() {
: sendWin = window.open(...);
: setTimeout("if (sendWin && !senWin.closed) sendWin.close()", 200);
:}
:
:... or you may use a closure, what means keeping sendWin
:accessible in an nested function (JS1.2+) ...
:
:function sendWindow() {
: var sendWin = window.open(...);
: setTimeout(function() {
: if (sendWin && !senWin.closed) sendWin.close();
: }, 200);
:}
:
:ciao, dhgm



Thank you! That worked great.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top