Close Child Window via Parent using setTimeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    Close Child Window via Parent using setTimeout

    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('ab out:blank','sen dwindow','width =100,height=100 ');
    setTimeout('sen dWin.close()',2 00);

    }

    Thank you
  • John Smith

    #2
    Re: Close Child Window via Parent using setTimeout

    I meant to write "sendWin is undefined". I apologize for any
    confusion.



    Comment

    • Dietmar Meier

      #3
      Re: Close Child Window via Parent using setTimeout

      John Smith wrote:
      [color=blue]
      > function sendWindow()
      > {
      > var sendWin =
      > window.open('ab out:blank','sen dwindow','width =100,height=100 ');
      > setTimeout('sen dWin.close()',2 00);
      >
      > }[/color]

      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(func tion() {
      if (sendWin && !senWin.closed) sendWin.close() ;
      }, 200);
      }

      ciao, dhgm

      Comment

      • John Smith

        #4
        Re: Close Child Window via Parent using setTimeout

        On Sat, 2 Apr 2005 14:47:36 +0200, "Dietmar Meier"
        <usereplytoinst ead@innoline-systemtechnik.d e> wrote:

        :John Smith wrote:
        :
        :> function sendWindow()
        :> {
        :> var sendWin =
        :> window.open('ab out:blank','sen dwindow','width =100,height=100 ');
        :> setTimeout('sen dWin.close()',2 00);
        :>
        :> }
        :
        :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(func tion() {
        : if (sendWin && !senWin.closed) sendWin.close() ;
        : }, 200);
        :}
        :
        :ciao, dhgm



        Thank you! That worked great.



        Comment

        Working...