Jeff Bigham wrote:
I'm trying to write an equivalent to the hasAttribute method that will
work with IE 6 and 7. IE 8 finally has this method built in.
Iff it is ever released.
There is supposed to be a way to do this using the 'specified'
property of the attribute, but it doesn't always work.
For instance, consider the following code:
function test() {
var intest = document.getElementById('in_test');
alert(intest.getAttribute('type') + " " +
intest.attributes['type'].specified + " " + intest.type);
}
...
<input id='in_test' type="text"></input>
This is not Valid HTML (and MSHTML does not support XHTML to date). Since
the content model of the INPUT element is EMPTY, the O(mit) flag for the end
tag means it is *forbidden*:
<http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.3>
<http://validator.w3.org/>
...
Calling test() alerts "text false text"
I think this is because IE 6/7 confuses user-defined attributes and
its own DOM properties. Although, since both say that type is equal
to "text", I have no idea why I'm getting false here.
One wonders instead how you can be getting anything at all in IE 6/7. The
`attributes' property is documented to be supported not before IE 8 beta:
<http://msdn.microsoft.com/en-us/library/cc304094(VS.85).aspx>
In other cases, IE requires odd workarounds. Like, you set the
className instead of class attribute to set the class attribute.
That is not really a workaround, though; `className' is a specified property
of HTML element objects and is supported by several other DOM
implementations as well:
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95362176>
The (known) issue is certainly in MSHTML's setAttribute(), however calling
setAttribute() is seldom necessary.
Is there something similar going on here? I tried 'typeName' but that
didnt' work.
So copy-and-pray is not a successful coding strategy? Tell us something new.
If that's the problem, does anyone know where I can find
a comprehensive list of all such name changes?
"Name changes"? RTFM.
Or, just in general, a reasonable way to tell if the user has
specified an attribute.
Given that all valid HTML attributes have corresponding attribute properties
in all known DOMs (CMIIW), it would be sufficient to test whether the
property value equals the specified or implemented initial value or not.
However, if you really need to know if the attribute was specified, use
getAttribute(). WFM.
<http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx>
<http://msdn.microsoft.com/en-us/library/ms761380(VS.85).aspx>
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>