472,364 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 67800
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
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
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
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
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
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
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
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
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
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
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.