Determine proper window by element.

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

    Determine proper window by element.

    Is there a simple and clean way to get the window object that contains
    an element that is passed into a function? I want to throw some alerts
    / confirms, but don't want to switch away from whatever window they're
    currently looking at.

    I can step up by parentNode until the nodeType equals 9, which means
    the document element, and then step up one more...does that ensure a
    window object? Or i could step up until the node had no
    parentNode...wo uld that be the window object?

    Any other ideas anyone?

    -Brendan

  • Danny

    #2
    Re: Determine proper window by element.


    The object 'window' from the event, refer to the current window, no need to
    walk Up to it.


    Danny

    Comment

    • Donius

      #3
      Re: Determine proper window by element.

      Hmm...a good thought...i can't just call event like this though:
      var cwin = event.window;

      because 'event is not defined'. I'm doing something where i have (a) a
      main page and (b) some number of windows that are opened from that
      page, and the opened windows can all call functions from the main page.
      So when i'm doing this, window.event doesn't hold the last event on
      one of the opened windows, it holds the last event on the main window,
      and i want the window where the event happened. Currently the
      functions pass just themselves (like: onclick="functi oncall(this);") .
      Is there a way to pass the event with the function? Does self.event
      make sense in that context?

      So, in general: How would i get the event?

      Comment

      • Danny

        #4
        Re: Determine proper window by element.


        Event is at the window/frame level, so, self.event is fine, however, event
        IS defined, in IE, but in mozilla you have to use the 1st argument or
        arguements[0] to get the event object in the function, what I've used is:

        function SOSO(ev) {
        if (window.externa l) ev=window.event ; // IE is the only
        browser with window.external object.
        .....
        }

        mozilla/geckos will use 'ev' as it comes as 1st argument off the event, IE
        will use window.event, or self.event if you wish, another thing is, yes,
        you can pass 'event' as an argument, so onclick="SOSO(t his.event)" should
        work too.


        Danny

        Comment

        • RobG

          #5
          Re: Determine proper window by element.

          Donius wrote:[color=blue]
          > Is there a simple and clean way to get the window object that contains
          > an element that is passed into a function?[/color]

          Yes. Pass a reference to the window when you call the function in the
          opener. e.g. in the child window do:

          opener.someFn(w indow, parm1, parm2);

          [color=blue]
          > I want to throw some alerts
          > / confirms, but don't want to switch away from whatever window they're
          > currently looking at.[/color]

          Use the alert (or prompt or whatever) method of the window whose
          reference you've been passed - quick 'n dirty script below:

          <script type="text/javascript">
          var newWin;
          function openWin()
          {
          newWin = window.open('', 'NewWindow',
          'menubar,locati on,resizable,sc rollbars,status ,'
          +'height=500,wi dth=600');

          newWin.document .open();
          newWin.document .write(
          '<html><title>C hild of NewWindow</title>',
          '<body><input type="button" value="Show alert" ',
          'onclick="opene r.parentAlert(w indow, \'hi from child\');">',
          '</body></html>'
          );
          newWin.document .close();
          newWin.focus();
          }

          function parentAlert( win, txt )
          {
          win.alert( 'Alert from opener: ' + txt );
          }

          </script>
          <input type="button" value="Open window" onclick="
          openWin();
          ">
          [color=blue]
          >
          > I can step up by parentNode until the nodeType equals 9, which means
          > the document element, and then step up one more...does that ensure a
          > window object? Or i could step up until the node had no
          > parentNode...wo uld that be the window object?[/color]

          Just use window, no DOM climbing required. I think the highest node is
          'document' or the HTML element, but it's not required.
          [color=blue]
          >
          > Any other ideas anyone?[/color]

          Yeah, but I think the one above is best.


          --
          Rob

          Comment

          • Donius

            #6
            Re: Determine proper window by element.

            Thanks to both Rob & Danny, that cleared things up. :) I started
            passing in the window object as my final solution. I appreciate the
            help!

            -Brendan

            Comment

            • RobG

              #7
              Re: Determine proper window by element.

              Danny wrote:[color=blue]
              > Event is at the window/frame level, so, self.event is fine, however, event
              > IS defined, in IE, but in mozilla you have to use the 1st argument or
              > arguements[0] to get the event object in the function, what I've used is:
              >
              > function SOSO(ev) {
              > if (window.externa l) ev=window.event ; // IE is the only
              > browser with window.external object.[/color]

              If you want to test for window.event, then test for exactly that:

              ev = ev ||window.ev
              [color=blue]
              > .....
              > }
              >
              > mozilla/geckos will use 'ev' as it comes as 1st argument off the event, IE[/color]

              It it will be whichever paramater you pass it as, it can be in any position.
              [color=blue]
              > will use window.event, or self.event if you wish, another thing is, yes,
              > you can pass 'event' as an argument, so onclick="SOSO(t his.event)" should[/color]

              onclick="SOSO(e vent);"

              [...]

              --
              Rob

              Comment

              Working...