473,503 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 67964
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15979
by: dan | last post by:
This works in mozilla but I can't figure out why this won't work in IE. Say I have this html <html> header goes here header goes here
3
2931
by: chris kramer | last post by:
i have an application that allows you to Select some text in a window, but no option to Copy it to the clipboard (nor does Ctrl-C or Shift-insert work, or right click etc..) i want to get these...
10
37972
by: Shang Wenbin | last post by:
Hi, When I want to close the current window using window.close() in IE6.0, there will be a confirm box that: The web page you are viewing is trying to close the window. Do you want to close this...
4
3581
by: Csaba Gabor | last post by:
Up until a few weeks ago, javascript code like window.open("http://mydomain.com", "windowName"); would always bring my new or reused window to the top, with focus. Lately, Firefox (Deer park...
22
129953
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
3
1733
by: Roger Withnell | last post by:
Is it possible to test if a window or frame is in focus? Posted Via Usenet.com Premium Usenet Newsgroup Services ---------------------------------------------------------- ** SPEED **...
3
4529
by: dibakar | last post by:
Hello I want to open a window using Window.showModalDialog once i click inside of a TextBox.And that window need to come just under the TextBox,so that it seems to a same control. So,what...
1
2672
by: =?Utf-8?B?amFtZXNAbm9zcGFtLmNvbQ==?= | last post by:
When processing the OnGotFocus event, I need to know the window/control that lost the focus. Is there a way to determint this? With MFC, you could use OnSetFocus which passed you the window that...
5
2559
by: kalyangvd | last post by:
Hi Guys, I am trying to get the Field Id from the Parent Window in the pop up window using window.opener ex: var var1 = window.opener.document.getElementById("text1").value; ...
2
6739
by: kurt sune | last post by:
Hello, I have a weird problem, I hope someone can explain this for me. I have a webpage using masterpage. In it I create a popup window using this code: Dim js As String = "<script...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.