By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,624 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,624 IT Pros & Developers. It's quick & easy.

Close Child Window via Parent using setTimeout

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I meant to write "sendWin is undefined". I apologize for any
confusion.

Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
On Sat, 2 Apr 2005 14:47:36 +0200, "Dietmar Meier"
<us***************@innoline-systemtechnik.de> wrote:

: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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.