View Full Version : Trying to set focus onLoad ?

Sep 4th, 2003, 08:32 PM
I am trying to set the focus on a text box named "SearchPhrase" when the form loads.

I have tried this syntax, but to no avail :

<FORM name="MySearch" METHOD="post" ACTION="do_Search.php" onLoad="document.forms[0].SearchPhrase.Focus();">

Anybody know how to use this rather trival method ?

JS seems to make the difficult trivial, and the trivial difficult. :(

Sep 4th, 2003, 08:38 PM
Have you tried:

<BODY onLoad="document.forms[0].SearchPhrase.focus();">
<FORM name="MySearch" METHOD="post" ACTION="do_Search.php">



Sep 4th, 2003, 08:41 PM
When you do this:

<FORM name="MySearch" METHOD="post" ACTION="do_Search.php" onLoad="document.forms[0].SearchPhrase.Focus();">

...the browser, when creating the Form object representing the HTML <form>, looks for a Form object property named 'onload' to assign your code string to. Naturally, there's no such property - which makes sense, as an HTML form has no need for an onload handler (when is the form 'loaded'?). OK - that's arguable.

You intended to assign code to run when the window.onload handler is called:

<body onload="document.forms[0].SearchPhrase.focus()">

Watch the case-sensitivity.

The <body> tag is utilized for this because, well, they couldn't find the damn <window> tag...;)

Sep 4th, 2003, 08:50 PM
Yes, but I am getting a JS error now :

"Object does not support the property or method." (I am using I.E. 5.5 here).

I'm just wondering, could this be due to the fact that when the page loads that JS knows nothing about this text box as yet?

Sep 4th, 2003, 08:53 PM
1) Is 'SearchPhrase' a text input?
2) Did you miss this:

<body onload="document.forms[0].SearchPhrase.focus()"> :D

Sep 4th, 2003, 08:54 PM
Yep, I incorrectly typed in "Focus" instead of "focus." :eek: It works fine now.

Geeze, why isn't JS as picky with "onload" vs. "onLoad"?

Sep 4th, 2003, 09:01 PM
It is! onLoad is HTML - not 'JavaScript' (actually DOM). Some browsers' JS parsers may buy the intercapped version, but they shouldn't. HTML is a simple markup language, very lenient in most executions. XHTML ain't so friendly, and scripting/programming languages are far more rigorous. A lot of people have come to JS from HTML, and are generally in for a rude awakening when it comes to precision.

cheers, cB

Sep 4th, 2003, 09:25 PM
. . . it should always be "onload" then?

I jumped from HTML right into PHP scripting, so I know what you mean about case sensitivity! I am now sort of back tracking into JS for client side stuff.

I really like this "PHP - JS" combination and feel I can do most anything that Java proper can do. Of course, the IDE's are not as robust (not yet anyway) but they are catching up. And PHP 5.0 is just now being released, with more OOP stuff in the works.

Thanks again for your input.

- - -

"Ask and ye shall receive. Knock and it shall be opened unto you."

Philip M
Apr 6th, 2009, 05:21 PM
Why have you revived this ancient thread?