By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Mar 8 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 8, 3:32 pm, Roger <crosseyedpeng...@cox.netwrote:
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

Mar 8 '07 #2

P: n/a
RobG wrote:
On Mar 8, 3:32 pm, Roger <crosseyedpeng...@cox.netwrote:
>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
Mar 8 '07 #3

P: n/a
On Mar 8, 9:39 am, Roger <crosseyedpeng...@cox.netwrote:
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 '07 #4

P: n/a
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? 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
You probably want to use

if(self.focus)
{
self.focus();
};

and then you may not need to do so when a secondary window is created
for the first time... a very typical error done in many scripts.

Pretty much every .focus() call has become suspect nowadays since a wide
majority of browsers in use these days are tab-capable browsers and each
and all the browsers you mention (Opera 9.10, Firefox 2.02, and IE 7)
allow users (in preferences settings) to cancel the raising of windows
and can force the opening of new secondary windows into the current tab
or into a new tab.

Gérard
--
Using Web Standards in your Web Pages (Updated Dec. 2006)
http://developer.mozilla.org/en/docs...your_Web_Pages
Mar 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.