Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Try:

document.onfocusout = window.close();

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

w.onblur = function() { w.close();};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top