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

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.

Advertisements

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").

Advertisements

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:

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?

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

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!