473,729 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Form Focus


I focus on controls in a web form like this:

<script language="javas cript">
var focusControl = null;
if (document.getEl ementById)
focusControl = document.getEle mentById('txtAr ea1');
else if (document.all)
focusControl = document.all['txtArea1'];
if (focusControl && focusControl.fo cus)
focusControl.fo cus();
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.

How can I make this perfect and foolproof? I've looked at my HTML
thousands of times and the textareas are like all the others that work
without an alert box.

Thanks.

Jul 23 '05 #1
3 3049
br************@ hotmail.com wrote:
I focus on controls in a web form like this:

<script language="javas cript">
The language attribute is depreciated, type is required:

<script type="text/javascript">

But that has nothing to do with your issue :-(
var focusControl = null;
if (document.getEl ementById)
focusControl = document.getEle mentById('txtAr ea1');
else if (document.all)
focusControl = document.all['txtArea1'];
if (focusControl && focusControl.fo cus)
focusControl.fo cus();
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.

How can I make this perfect and foolproof? I've looked at my HTML
thousands of times and the textareas are like all the others that work
without an alert box.


Are you running this from the page as it loads?

It may well be that the browser has not finished creating 'txtArea1'
when the script is called, so focus goes to the address bar. The
alert gives the browser time to finish the page and do the focus.

Try running the script using body onload and see if that helps.
--
Rob
Jul 23 '05 #2
br************@ hotmail.com wrote:
I focus on controls in a web form like this:

<script language="javas cript">
<script type="text/javascript">

see numerous explanations in this group.
var focusControl = null;
if (document.getEl ementById)
focusControl = document.getEle mentById('txtAr ea1');
else if (document.all)
focusControl = document.all['txtArea1'];
If you use document.all, use it as a method (parens), not as a collection
(brackets).
if (focusControl && focusControl.fo cus)
focusControl.fo cus();
Regarding feature detection this is a valid approach in most cases, but
could result in a script error anyway; it only tests whether that host
object property exists, not if it is a method (property of type "function")
as required by the Call Operator `(...)'. Therefore, I have developed
an efficient and easy-to-use method for testing methods some time ago.
Feel free to use it if you take heed of the License:

/**
* @author
* (C) 2003, 2004 Thomas Lahn &lt;ty******@Po intedEars.de&gt ;
* Distributed under the GNU GPL v2 and above.
* @optional Object|string o
* Object to be determined an method, i.e. a
* <code>Functio n</code> object assigned as property of
* another object. May also be a string to be evaluated
* and so is applicable to unknown properties.
* @return type boolean
* <code>true</code> if <code>o</code> is a method,
* <code>false</code> otherwise.
* @see #isMethodType()
*/
function isMethod(m)
{
var t;
(m = eval(m)) && (t = typeof m);
return (t == "function" || t == "object");
}

You would use it here like this:

if (focusControl && isMethod(focusC ontrol.focus))
{
focusControl.fo cus();
}
</script>

This works on all of my textboxes and textareas except for 2. For them,
if I put a "alert(focusCon trol.id);" after the "focusControl.f ocus();",
then after the user clicks Enter the controlg gets focus. But if I
don't put that alert in there then the address bar (?!) gets focus.
You should post the source code of those two textboxes/textareas if the
below does not help:
How can I make this perfect and foolproof? [...]
Whenever forms are involved, try to avoid getElementById( ), document.all()
aso. and use DOM Level 0 downwards compatible referencing:

focusControl = document.forms[...].elements[...];

This is downwards compatible with names and works in current UAs with IDs
as well, see the W3C DOM Level 2 HTML Specification.
I've looked at my HTML thousands of times and the textareas are like all
the others that work without an alert box.


Can't be. However, you should not continue that practice at all: Focusing
a control with client-side scripting almost always prevents the user from
scrolling the respective document with non-pointing devices, such as a
keyboard.
PointedEars
Jul 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
br************@ hotmail.com wrote:

<snip>
focusControl = document.all['txtArea1'];


If you use document.all, use it as a method (parens),
not as a collection (brackets).

<snip>

Quite the reverse. There are browsers implementing the - document.all -
collection exclusively as an object (rather than as a function object)
so treating the - all - collection as a function will error in those
environments, while all the browsers where the - all - collection can be
called also allow it to be treated as an object, and property accessors
used. And referencing a member of the - all - collection with a property
accessor should be faster as there are inevitable overheads in a
function call.

Generally no collection should be treated as a function, despite the
fact that doing so is usually effective in IE (and directly proposed in
much Microsoft documentation [1]). It is a bad habit to ever get into
and, if done habitually, will become needless barrier to cross-browser
scripting.

Richard.

[1] Microsoft often seem to take the stance that they have won the
browser war, so it is not particularly surprising to find their
documentation proposing practices that would act against cross-browser
coding.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6728
by: Iver Erling Årva | last post by:
I have this window where I open a modal dialog window using the code: newwin=window.showModalDialog(url, "popup", features) where features are dialogLeft:"+x+"px;dialogTop:"+y+"px;dialogWidth:"+w+"px;dialogHeight:"+h+"p x;center:0"
3
1644
by: Marc Llenas | last post by:
Hi there, I'm stuck on a validation function for a form and I cannot figure out what the problem is. The page is in ASP. Any ideas? The function being called is: <script language="JavaScript" type="text/javascript"> function checkform ( form ) {
1
5049
by: cider123 | last post by:
I've tried working with the SelectedIndices and Items.Selected attributes to get the problem to go away, but not having any luck. Questions I have are: 1) How do you move (using code) the focus Rectangle when your Selected Index changes ? 2) If there are no selected rows, can that focus rectangle be removed? 3) What other quirks exist in this simple example I might need to code for ?
1
2242
by: avnrao | last post by:
Hi, I am facing a problem with control.focus (javascript). Here is the description of the issue. 1. I have 2 aspx files. on Aspx1 I have button named NewRow. Clicking on this, will redirect page to Aspx2 which has a Datagrid. PageLoad of Aspx2 displays the DataGrid with existing data filled in and shows a new row (set of text boxes) in the Footer Item. Now, when I set the focus by accessing the first text box in the Footer
18
1802
by: Derek Martin | last post by:
Hi there, this is probably really dumb, but I am using a dialog as my main form and my startup form is my splash screen - which implements some progress bars to load up some relevant data. When that is done, loading form does this: Me.Visible = False mainform.ShowDialog() Application.Exit() So far so good (is this a dumb way of doing that???). Now, I am
0
1879
by: Bruin | last post by:
Hi All, I'm having a problem with MDI child forms when the reference to the MDI Parent is set in a Control library. (Sorry for the long post) I have an control library assembly which holds all of my base classes including my base MDI Container form and my base MDI child form the mdi container has a singleton which returns an
1
5135
by: clickon | last post by:
For testing purposes i have got a 2 step WizardControl. Eqach step contains a text box, TextBox1 and TextBox2 respectively. If i put the following code in the respective activate event handlers for the two steps, TextBox1.Text ="foo"; and TextBox2.Text = "bar";
21
5100
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main form, the code errors out at the 2nd line. The error number is 13 and the description is "Type mismatch". Both controls are memo fields. I suspect that "Screen.ActiveControl.Parent" is not referencing the form as intended. Thanks for any help,...
3
4249
by: jan.loucka | last post by:
Hi, I looked around for this specific problem but could not find any answer - there's few things in VB but still nothing exactly like this so I'd appreciate any help. We're writing C# WinForm application that need to "silently" use another app (called MapInfo) within itself - the intention is to use this app (which is quite sophisticated mapping application) and add some other extra functionality to it. We have a dll so we can run the...
0
9436
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9297
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9224
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9164
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4537
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3248
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 we have to send another system
2
2703
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.