From: AKS on 21 Dec 2007 23:52 On Dec 22, 4:10 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > The logical course of action should be clear then: "should be clear"? Not absolutely. What do you check here: > if (o && isMethod(o, "focus") > && typeof o.style == "undefined" > || typeof o.style.visibility != "undefined") ? To avoid an error, it should look like this: if (o && isMethod(o, "focus") && typeof o.style != "undefined" && typeof o.style.visibility != "hidden") But in IE it is still dangerous. Because style can be specified in css or can be inherited from the parent (hidden) element. Then your check is useless. > One might also want to employ exception handling (try...catch)... And one might (who don't care about IE older 5.5) also want to employ this safety method: o.setActive();
From: Randy Webb on 22 Dec 2007 01:26 AKS said the following on 12/21/2007 11:52 PM: > On Dec 22, 4:10 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> > wrote: > >> The logical course of action should be clear then: > > > "should be clear"? Not absolutely. What do you check here: Not a lot Thomas does is "clear" to anyone but Thomas. >> if (o && isMethod(o, "focus") >> && typeof o.style == "undefined" >> || typeof o.style.visibility != "undefined") > > ? It is testing to attempt to ensure that the UA supports a "style" property on 'o' and that it supports a 'visibility' property on that style property. It does make one wonder why he passed an un-used parameter and didn't test to make sure it wasn't type="hidden". > To avoid an error, it should look like this: > > if (o && isMethod(o, "focus") && > typeof o.style != "undefined" && > typeof o.style.visibility != "hidden") With your own correction of removing the typeof, that test doesn't mean a whole lot. If the browser doesn't support the visibility property - in script but does in CSS - then your test will merely add a visibility property to the style property. Even then, try testing your "modified" code with an input type="hidden". > But in IE it is still dangerous. Because style can be specified in css > or can be inherited from the parent (hidden) element. Then your check > is useless. No, because that is not what it is checking. It isn't checking the state of the property, it is checking for the existence of that property. Although it should do both. Not a lot of sense in focusing a hidden field. Or, one of type="hidden". >> One might also want to employ exception handling (try...catch)... > > And one might (who don't care about IE older 5.5) also want to employ > this safety method: > > o.setActive(); You need to revise that limitation to "if you don't care about anything but IE6/7". Try testing it in a non-IE browser. Error: document.getElementById("that").setActive is not a function Still, not a lot of sense in making an element "active" if it is hidden. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
From: Randy Webb on 22 Dec 2007 01:30 Thomas 'PointedEars' Lahn said the following on 12/21/2007 6:10 PM: > 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() function focusElementById(elemID){ > { > var o = document.getElementById("search"); var o = document.getElementById(elemID); > if (o && isMethod(o, "focus") > && typeof o.style == "undefined" > || typeof o.style.visibility != "undefined") && o.type != "hidden" > { > o.focus(); > } > } > </script> > </head> > > <body onload="focusElementById('search');"> What's the point of passing a parameter if you aren't going to use it? > ... > </body> > > More feature-testing might be indicated. No "might" to it. <input type="hidden" id="search"> Test it. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
From: AKS on 22 Dec 2007 01:51 On Dec 22, 11:26 am, Randy Webb <HikksNotAtH...(a)aol.com> wrote: > > Error: document.getElementById("that").setActive is not a function It's for IE only (other browsers do not throw an error). (o.setActive || o.focus)(); // in short > Still, not a lot of sense in making an element "active" if it is hidden. > You never know in what environment the script will work. I've noticed IE feature and no more .
From: Randy Webb on 22 Dec 2007 01:59
AKS said the following on 12/22/2007 1:51 AM: > On Dec 22, 11:26 am, Randy Webb <HikksNotAtH...(a)aol.com> wrote: > >> Error: document.getElementById("that").setActive is not a function > > It's for IE only (other browsers do not throw an error). It won't? The error message I posted was a direct copy/paste from Firefox. > (o.setActive || o.focus)(); // in short Why though? Just use focus. >> Still, not a lot of sense in making an element "active" if it is hidden. >> > > You never know in what environment the script will work. I've noticed > IE feature and no more . IE will setActive() on a hidden input (type="hidden"). It will throw an error if you try to set focus() to it. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |