Using onblur to detect loss of window focus

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

    Using onblur to detect loss of window focus

    I'm struggling with the JavaScript blur event. I have an activex
    control that I need to interact with when a window loses focus, namely
    to instruct it to give up control of a barcode scanner so that the
    application being brought foreward can take control.

    It has been my observation that when I set the window.onblur function,
    the window will gain focus and then immediately lose focus (blur) to
    the first field, button, or whatever that is contained within the
    window. I can observe a text field gaining and losing focus as I
    switch between applications but its parent window will *never* receive
    any blur events as I switch between applications, so long as the
    cursor remains in the text field.

    Is there any way with JavaScript to know when the user is switching
    focus from the browser window to some other application without having
    to code logic into every friggin field on the page?

    Barry
  • Thomas 'PointedEars' Lahn

    #2
    Re: Using onblur to detect loss of window focus

    Barry Svee wrote:
    [color=blue]
    > I'm struggling with the JavaScript blur event.[/color]

    There are no JavaScript events, JavaScript itself neither provides
    events nor event handling. Events and event handlers are provided
    by the DOM of the used host, e.g. a HTML user agent.
    [color=blue]
    > I have an activex control that I need to interact with when a window[/color]
    ^^^^^^^^^^^^^^^[color=blue]
    > loses focus, [...][/color]

    And the IE browser component (IE) does not support JavaScript.
    It supports JScript, Microsoft's implementation of ECMAScript,
    which is nevertheless on-topic here. (See the FAQ.)
    [color=blue]
    > It has been my observation that when I set the window.onblur function,[/color]

    To *what*? That is important.
    [color=blue]
    > the window will gain focus and then immediately lose focus (blur) to
    > the first field, button, or whatever that is contained within the
    > window.[/color]

    I fail to observe that in my IE 6.0 SP-1 on Win2k (SP-4). Instead, I
    observe that if a browser window is focused and the focus comes from
    another application window, "onfocus" fires. If the focus comes from
    another IE window, sometimes "onfocus", "onblur" and "onfocus" fire
    (in that order) and sometimes only "onfocus" (the former is alas not
    reproducible). No matter if the focus is lost in favor of another
    application window or another IE window, "onblur" fires once.

    Test Code was

    javascript:void (window.onblur= function() {
    document.body.a ppendChild(docu ment.createText Node("; onblur")); });
    void(window.onf ocus=function() {
    document.body.a ppendChild(docu ment.createText Node("; onfocus")); })

    applied to about:blank.
    [color=blue]
    > Is there any way with JavaScript to know when the user is switching
    > focus from the browser window to some other application[/color]

    No, there is not, unless the IE-DOM provides a property that I do not
    know of. Maybe you can find it in

    [color=blue]
    > without having to code logic into every friggin field on the page?[/color]

    You need an event listener (function) for the "focus" event and apply it
    to every window of your application, i.e. every displayed document of
    it. The challenge you have to face is that the browser windows know
    each other only if they are opened through window.open(... ). You can
    then use the "opener" property of a window to reference the window that
    opened it. In that first window you could maintain a list of references
    to window objects as elements of an array or properties of a
    user-defined object and thus reference the window to be changed on an event.


    PointedEars

    Comment

    Working...