views:

1291

answers:

3

I want a transient window to close itself when the user clicks away from it. This works for Firefox:

var w = window.open(...);
dojo.connect(w, "onblur", w, "close");

but it doesn't seem to work in Internet Explorer. Some other sites made reference to an IE-specific "onfocusout" event, but I couldn't find a coherent working example of what I need.

What does Stack Overflow say about the best way to get IE browser windows to close when they lose focus?

I'm using Dojo so if there's some shortcut in that library, information would be welcome. Otherwise standard IE calls will be the best answer.

A: 

You might try this as part of an IE-specific code block:

w.onblur = function() { w.close();};
Randolpho
That's essentially what I have. It works for Firefox but not IE. IE doesn't fire the event at the appropriate times.
Steven Huwig
A: 

Try:

document.onfocusout = window.close();
w4g3n3r
That's the wrong document AFAICT (the document of the parent window). I think I tried w.document too and it didn't work. I don't have IE at home so I'll double-check tomorrow.
Steven Huwig
Sorry. I was assuming you had access to the page that is being loaded in the pop up. This code would go there.
w4g3n3r
+2  A: 

I figured out the alternative in IE.

This:

        that.previewWindowAction = function () {
            var pw =
                window.open(this.link, "preview",
                            "height=600,width=1024,resizable=yes,"
                            + "scrollbars=yes,dependent=yes");
            dojo.connect(pw, "onblur", pw, "close");
        };

should be written like this to work in IE:

        that.previewWindowAction = function () {
            var pw =
                window.open(this.link, "preview",
                            "height=600,width=1024,resizable=yes,"
                            + "scrollbars=yes,dependent=yes");
            if (dojo.isIE) {
                dojo.connect
                    (pw.document,
                     "onfocusin",
                     null,
                     function () {
                         var active = pw.document.activeElement;
                         dojo.connect
                             (pw.document,
                              "onfocusout",
                              null,
                              function () {
                                  if (active != pw.document.activeElement) {
                                      active = pw.document.activeElement;
                                  } else {
                                      window.open("", "preview").close();
                                  }
                              });
                     });

            }
            else {
                dojo.connect(pw, "onblur", pw, "close");
            }
        };

The reasons?

  • In IE, window objects do not respond to blur events. Therefore we must use the proprietary onfocusout event.
  • In IE, onfocusout is sent by most HTML elements, so we must add some logic to determine which onfocusout is the one caused by the window losing focus. In onfocusout, the activeElement attribute of the document is always different from the previous value -- except when the window itself loses focus. This is the cue to close the window.
  • In IE, documents in a new window send an onfocusout when the window is first created. Therefore, we must only add the onfocusout handler after it has been brought into focus.
  • In IE, window.open does not appear to reliably return a window handle when new windows are created. Therefore we must look up the window by name in order to close it.
Steven Huwig