this.window.focus() vs. window.focus() vs. this.focus()

Discussion in 'Javascript' started by Roger, Mar 8, 2007.

  1. Roger

    Roger Guest

    Hi,

    I am confused about the differences between this.window.focus(),
    window.focus(), and this.focus().

    I want to use the calls in a <body onload="..."> tag. What are the
    differences between these forms that may make one succeed and another
    fail? In particular, this.window.focus() fails in Opera 9.10 with an
    "object not found", and windows.focus() succeeds in Opera 9.10, Firefox
    2.02, and IE 7.

    Roger
     
    Roger, Mar 8, 2007
    #1
    1. Advertising

  2. Roger

    RobG Guest

    On Mar 8, 3:32 pm, Roger <> wrote:
    > Hi,
    >
    > I am confused about the differences between this.window.focus(),
    > window.focus(), and this.focus().
    >
    > I want to use the calls in a <body onload="..."> tag. What are the
    > differences between these forms that may make one succeed and another
    > fail?


    this.window.focus()
    When used in-line in the body tag, 'this' refers to the body element.
    In a standard DOM, the body element doesn't have a window property;
    trying to call a method of a non-existent property will result in a
    script error.

    window.focus()
    This calls the focus method of the window object, which should work in
    standards-compliant browsers though it may not have any effect. Some
    browsers provide user configurable settings so that users can prevent
    script from raising or lowering windows.

    this.focus()
    This will attempt to call the focus method of the body element. The
    W3C DOM 2 HTMLBodyElement interface doesn't define a focus method,
    therefore it's likely to fail in most (if not all) browsers.


    > In particular, this.window.focus() fails in Opera 9.10 with an
    > "object not found", and windows.focus() succeeds in Opera 9.10, Firefox
    > 2.02, and IE 7.


    Entirely expected (allowing for the typo of "windows.focus" rather
    than "window.focus").


    --
    Rob
     
    RobG, Mar 8, 2007
    #2
    1. Advertising

  3. Roger

    Roger Guest

    RobG wrote:
    > On Mar 8, 3:32 pm, Roger <> wrote:
    >> Hi,
    >>
    >> I am confused about the differences between this.window.focus(),
    >> window.focus(), and this.focus().
    >>
    >> I want to use the calls in a <body onload="..."> tag. What are the
    >> differences between these forms that may make one succeed and another
    >> fail?

    >
    > this.window.focus()
    > When used in-line in the body tag, 'this' refers to the body element.
    > In a standard DOM, the body element doesn't have a window property;
    > trying to call a method of a non-existent property will result in a
    > script error.
    >
    > window.focus()
    > This calls the focus method of the window object, which should work in
    > standards-compliant browsers though it may not have any effect. Some
    > browsers provide user configurable settings so that users can prevent
    > script from raising or lowering windows.
    >
    > this.focus()
    > This will attempt to call the focus method of the body element. The
    > W3C DOM 2 HTMLBodyElement interface doesn't define a focus method,
    > therefore it's likely to fail in most (if not all) browsers.
    >
    >
    >> In particular, this.window.focus() fails in Opera 9.10 with an
    >> "object not found", and windows.focus() succeeds in Opera 9.10, Firefox
    >> 2.02, and IE 7.

    >
    > Entirely expected (allowing for the typo of "windows.focus" rather
    > than "window.focus").
    >
    >
    > --
    > Rob
    >


    Thank you. If you write a book I will buy a copy.

    The use for the above is to open a popup window with help info for a
    complicated form. The intended use is for the user to read the help,
    close the window and continue filling out the form.

    In some cases, the user will click the parent window after reading the
    help info and hide the help window. Since I am using named windows to
    avoid users opening multiple copies of the same popup help window, some
    means of giving focus to an old copy of the help window is necessary,
    else the user will click the help link and nothing appears to happen.

    My googling has found advice for yet another variation, that is to add
    the focus call to the script opening the window. Something like:

    url = 'someURL';
    newwindow = window.open(url,'myhelp','height=600,....top=0');
    newwindow.focus();

    The above would appear to have no advantage over the <body onload...>
    variation. Is there some obscure advantage to doing it this way --
    would the help window gain focus faster if it takes a "long" time to
    load? If the user's browser denies a script from lowering/raising
    windows then all methods will fail equally, right?

    Roger
     
    Roger, Mar 8, 2007
    #3
  4. Roger

    Guest

    On Mar 8, 9:39 am, Roger <> wrote:
    > The use for the above is to open a popup window with help info for a
    > complicated form. The intended use is for the user to read the help,
    > close the window and continue filling out the form.
    >
    > In some cases, the user will click the parent window after reading the
    > help info and hide the help window. Since I am using named windows to
    > avoid users opening multiple copies of the same popup help window, some
    > means of giving focus to an old copy of the help window is necessary,
    > else the user will click the help link and nothing appears to happen.


    use divs (like on yahoo new account page) but if you have to use
    window popups :
    --
    url='someUrl'
    newWin=window.open(url,"helpWin","features")
    newWin.window.focus
     
    , Mar 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    4
    Views:
    1,644
    Jason
    May 7, 2007
  2. Randell D.
    Replies:
    8
    Views:
    230
    keyur shah
    Nov 6, 2003
  3. Replies:
    2
    Views:
    346
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  4. Csaba Gabor

    focus on the new window.open window

    Csaba Gabor, Nov 30, 2005, in forum: Javascript
    Replies:
    4
    Views:
    220
    Csaba Gabor
    Dec 1, 2005
  5. Spizzat2
    Replies:
    2
    Views:
    190
    Spizzat2
    Jan 9, 2008
Loading...

Share This Page