AKS said:
"should be clear"? Not absolutely. What do you check here:
?
The element object must have a focus() method. If it has, then either it
does not support a `style' object or if it does, it has to support the
`visibility' property.
To avoid an error, it should look like this:
if (o && isMethod(o, "focus") &&
typeof o.style != "undefined" &&
typeof o.style.visibility != "hidden")
I took into account that the element may be focused if there was no CSS
scripting support (with the assumption that then there was no CSS support
and the element could not be hidden), while your "correction" would not
allow the element to receive focus then.
But in IE it is still dangerous. Because style can be specified in css
It would already "be specified in css" in the `style' attribute value of the
element. You mean instead "specified in a document-global or linked
stylesheet".
or can be inherited from the parent (hidden) element.
Then your check is useless.
True. However, that occasion is something that can be tested for as well.
Quick hack:
function _getComputedStyle(o, p)
{
var s;
if (typeof document.defaultView != "undefined"
&& isMethod(document.defaultView, "getComputedStyle")
&& (s = document.defaultView.getComputedStyle(o, null))
&& isMethod(s, "getPropertyValue"))
{
return s.getPropertyValue(p);
}
else if (typeof o.currentStyle != "undefined"
&& typeof o.currentStyle[p] != "undefined")
{
return o.currentStyle[p];
}
return s;
}
function isHidden(o)
{
while (o)
{
if (typeof o.style == "undefined"
|| typeof o.style.visibility != "undefined"
&& /hidden/i.test(o.style.visibility)
|| /hidden/i.test(_getComputedStyle(o, "visibility"))
{
return true;
}
o = o.parentNode;
}
return false;
}
function focusElement(s)
{
var o = document.getElementById(s), s;
if (o && isMethod(o, "focus") && !isHidden(o))
{
o.focus();
}
}
Nevertheless, when observing this much of (feature) testing for one simple
call, this is one of the occasions where I wish that there was a universally
supported and easily scriptable DOM interface -- or at least that exception
handling was universally supported, for
try
{
o.focus();
}
catch (e)
{
}
is simply much more beautiful than the above, while the former might not
even suffice (consider `disabled').
And one might (who don't care about IE older 5.5) also want to employ
this safety method:
o.setActive();
I don't see why that would qualify as a "safety method". It does a
completely different thing:
| setActive Method
|
| Sets the object as active without setting focus to the object.
PointedEars