[Pmwiki-users] An idea before I forget: Search form in the search results page
Christian Ridderström
chr
Thu Jan 8 03:40:25 CST 2004
On Tue, 6 Jan 2004, dave.jackson wrote:
> Adding the following after the closing form tag sets the focus if your
> browser is IE (others too maybe?)
>
> <script language=\"vbscript\">document.all.text.focus</script>
Thanks... but I'm no big fan of vb :-(. It does work in IE, but it does
not work in e.g. Opera. Furthermore, when I tried validating the
result (very simple in Opera, just press A-C-v :-), I got this error:
Line 18, column 30: required attribute "TYPE" not specified (explain...).
<script language='vbscript' >document.all.text.focus</script>
although it was easily fixed:
<script language='vbscript' type='text/vbscript'>document.all.text.focus</script>
However, I did a bit of searching and found this link:
http://javascript.about.com/library/scripts/blfirstfocus.htm
which I modified into the snippet of code that can be added anywhere after
the form. (It works in Opera and IE, and should work in most other browsers)
<script language='javascript' type='text/javascript'>
var TForm = document.forms[0];
for (i=0; i<TForm.length; i++) {
if ((TForm.elements[i].type==\"text\")||
(TForm.elements[i].type==\"textarea\")||
(TForm.elements[i].type.toString().charAt(0)==\"s\")) {
document.forms[0].elements[i].focus();
break;
}
}
</script>
However, if you want to save a few bytes, it's enough to put this snippet
of code somewhere after the form-element:
<script language='javascript' type='text/javascript'>
document.forms[0].elements[1].focus();</script>
but note that it's hardcoded to focus on the 2nd element in the 1st form,
and doesn't check that it's the right kind of element.
/Christian
--
Dr. Christian Ridderstr?m, +46-8-768 39 44 http://www.md.kth.se/~chr
More information about the pmwiki-users
mailing list