View Full Version : Input field active - setting the focus on page load
Is there a way to make the input field 'active' as soon as the page opens - whether it's the main form page or just a simple form?
Tim
wrensoft
01-12-2005, 07:00 PM
The browser will normally do this for you. So it is not normally required.
However if you want to change the default browser behaviour you need to use some additional Javascript code to do this.
For the sake of an example, imagine that this was your Zoom search form,
<form method="GET" name="MySearchForm" action="search.php">
<input type="text" name="zoom_query" size="15">
<input type="submit" value="Search" name="Button1">
</form>
Then you could use Javascript like this to set the position of the cursor (the focus) by adding an onLoad attribute to the <BODY ...> tag.
<BODY onLoad="document.forms.MySearchForm.zoom_query.focus()">
--------
David
WrenSoft
webby
04-10-2006, 11:13 AM
I am trying to achieve the same effect as tj, however, I am using .asp. The browser does not default the cursor either, as was suggested by David and although I have tried applying David's solution (in the body of search_template.html file) I do not get a cursor defaulting to the search box ready for users to input their text.
In the search.asp file the form is not named :-
Response.Write("<form method=""get"" action=""" & selfURL & """ class=""zoom_searchform"">") & VbCrlf
Response.Write(STR_FORM_SEARCHFOR & " <input type=""text"" name=""zoom_query"" size=""20"" value=""" & encQuery & """ class=""zoom_searchbox"" tabindex=""0"" />") & VbCrlf
Any suggestions on how this could be achieved? I have viewed in IE, Netscape 6.1 and Firefox and get the same results with all.
webby
04-10-2006, 02:17 PM
I thought I'd try adding the missing form name and apply David's solution to the search_template.html file and hey presto I now have the cursor blinking away in the search input box! (I remembered to use ZoomIndexer to make the change to the search.asp file)
Response.Write("<form method=""get"" name=""MySearchForm"" action=""" & selfURL & """ class=""zoom_searchform"">") & VbCrlf
Response.Write(STR_FORM_SEARCHFOR & " <input type=""text"" name=""zoom_query"" size=""20"" value=""" & encQuery & """ class=""zoom_searchbox"" tabindex=""0"" />") & VbCrlf
I hope this helps anyone else who has encountered a similar need.
David's suggestion above was for search forms created by the user (http://www.wrensoft.com/zoom/support/faq_howto.html#searchform) (in HTML).
An alternative method that would not require a form name (and thus, not require editing the search script) would be the following:
<BODY onLoad="document.getElementById('zoom_query').focus()">
For more information on using Javascript with forms, check out this page (http://home.cogeco.ca/~ve3ll/jstutor4.htm) (also try a search on Google).
webby
04-11-2006, 11:05 AM
Ray
Thank you for this line of code and the referral. I will look into converting to this method in the future.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.