From: ibizara on 21 Dec 2007 09:13 <script type="text/javascript"> function formfocus() { document.getElementById('search').focus(); } window.onload = formfocus; </script> I'm looking to add ¬ "is there is a element with the id search if so auto focus" else "do nothing" Many Thanks to anyone wishing to help, ibizara
From: Bart Van der Donck on 21 Dec 2007 11:08 ibizara wrote: > <script type="text/javascript"> > function formfocus() { > document.getElementById('search').focus(); > } > window.onload = formfocus; > </script> > > I'm looking to add > "is there is a element with the id search if so auto focus" > else > "do nothing" <body onLoad=" if (document.getElementById('search')) document.getElementById('search').focus(); "> Hope this helps, -- Bart
From: AKS on 21 Dec 2007 12:38 On 21 ÄÅË, 21:08, Bart Van der Donck <b...(a)nijlen.com> wrote: > > <body onLoad=" > if (document.getElementById('search')) > document.getElementById('search').focus(); > "> > > Hope this helps, It's not enough. In IE there can be an error. Check this: <body onload=" if (document.getElementById('search')) document.getElementById('search').focus(); "> <input type='text' id='search' style='visibility:hidden;'>
From: Thomas 'PointedEars' Lahn on 21 Dec 2007 18:10 AKS wrote: > On 21 дек, 21:08, Bart Van der Donck <b...(a)nijlen.com> wrote: >> <body onLoad=" >> if (document.getElementById('search')) >> document.getElementById('search').focus(); >> "> >> >> Hope this helps, > > It's not enough. In IE there can be an error. Check this: > > <body onload=" > if (document.getElementById('search')) > document.getElementById('search').focus(); > "> > > <input type='text' id='search' style='visibility:hidden;'> The logical course of action should be clear then: <script type="text/javascript"> function isMethod(o, p) { return /\b(function|object)\b/i.test(o[p]) && o[p]; } function focusElementById() { var o = document.getElementById("search"); if (o && isMethod(o, "focus") && typeof o.style == "undefined" || typeof o.style.visibility != "undefined") { o.focus(); } } </script> </head> <body onload="focusElementById('search');"> ... </body> More feature-testing might be indicated. One might also want to employ exception handling (try...catch) in case there are further conditions that have to be met in order for the element to receive the focus which cannot be tested for. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on 21 Dec 2007 18:11
Thomas 'PointedEars' Lahn wrote: > return /\b(function|object)\b/i.test(o[p]) && o[p]; return /\b(function|object)\b/i.test(typeof o[p]) && o[p]; |