Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 06:48 PM
Jeff Bigham
Guest
 
Posts: n/a
Default hasAttribute equivalent for IE

Hi,

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.

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>
....

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.

In other cases, IE requires odd workarounds. Like, you set the
className instead of class attribute to set the class attribute. Is
there something similar going on here? I tried 'typeName' but that
didnt' work. If that's the problem, does anyone know where I can find
a comprehensive list of all such name changes?

Or, just in general, a reasonable way to tell if the user has
specified an attribute.

Thanks,
Jeff
  #2  
Old September 4th, 2008, 11:35 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: hasAttribute equivalent for IE

Jeff Bigham wrote:
Quote:
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.
Quote:
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/>
Quote:
...
>
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>
Quote:
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.
Quote:
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.
Quote:
If that's the problem, does anyone know where I can find
a comprehensive list of all such name changes?
"Name changes"? RTFM.
Quote:
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>
  #3  
Old September 5th, 2008, 05:15 PM
David Mark
Guest
 
Posts: n/a
Default Re: hasAttribute equivalent for IE

On Sep 4, 6:29*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
Jeff Bigham wrote:
Quote:
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.
>
Quote:
There is supposed to be a way to do this using the 'specified'
property of the attribute, but it doesn't always work.
>
Quote:
For instance, consider the following code:
>
Quote:
function test() {
* * var intest = document.getElementById('in_test');
* * alert(intest.getAttribute('type') + " " +
intest.attributes['type'].specified + " " + intest.type);
}
>
Quote:
...
>
Quote:
<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/>
>
Quote:
...
>
Quote:
Calling test() alerts "text false text"
>
Quote:
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>
>
Quote:
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.
>
Quote:
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.
>
Quote:
If that's the problem, does anyone know where I can find
a comprehensive list of all such name changes?
>
"Name changes"? *RTFM.
>
Quote:
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.
In IE, the getAttribute method is broken (as designed) just like
setAttribute (same as reading/writing properties.)

I believe there is a way to determine specified attributes in IE, but
as noted, it is rarely useful. See my has/get/set/Attribute wrappers.
  #4  
Old September 7th, 2008, 11:15 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: hasAttribute equivalent for IE

David Mark wrote:
Quote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Quote:
>[...]
>However, if you really need to know if the attribute was specified, use
>getAttribute(). WFM.
>
In IE, the getAttribute method is broken (as designed) just like
setAttribute (same as reading/writing properties.)
My admittedly non-exhaustive tests did not confirm that for the MSHTML DOM
or the MSXML DOM, though. Please elaborate.


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@news.demon.co.uk>
  #5  
Old September 8th, 2008, 02:35 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: hasAttribute equivalent for IE

Thomas 'PointedEars' Lahn wrote:
Quote:
David Mark wrote:
Quote:
>[...] Thomas 'PointedEars' Lahn [...] wrote:
Quote:
>>[...]
>>However, if you really need to know if the attribute was
>>specified, use getAttribute(). WFM.
>>
>In IE, the getAttribute method is broken (as designed)
>just like setAttribute (same as reading/writing properties.)
>
My admittedly non-exhaustive tests did not confirm that for the
MSHTML DOM or the MSXML DOM, though. Please elaborate.
The DOM Core - getAttribute - method is supposed to always return a
string, yet with:-

<html>
<head>
<title></title>
<script type+"text/javascript">
window.onload = function(){
var el = document.getElementById('test');
alert('onclick = '+(typeof el.getAttribute('onclick')));
}
</script>
</head>
<body>
<input type="button"
value="Test"
onclick="alert('intrinsic event code');"
id="test"
Quote:
>
</body>
</html>

- the onload alert shows 'function' not 'string', which is certainly
wrong.

Similarly:-

<html>
<head>
<title></title>
<script type+"text/javascript">
window.onload = function(){
var el = document.getElementById('test');
alert('checked = '+(typeof el.getAttribute('checked')));
}
</script>
</head>
<body>
<input type="checkbox"
checked
id="test"
Quote:
>
</body>
</html>

- shows 'boolean' for the checked attribute.

Richard.

  #6  
Old September 8th, 2008, 11:45 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: hasAttribute equivalent for IE

Richard Cornford wrote:
Quote:
Thomas 'PointedEars' Lahn wrote:
Quote:
>David Mark wrote:
Quote:
>>[...] Thomas 'PointedEars' Lahn [...] wrote:
>>>[...]
>>>However, if you really need to know if the attribute was
>>>specified, use getAttribute(). WFM.
>>In IE, the getAttribute method is broken (as designed)
>>just like setAttribute (same as reading/writing properties.)
>My admittedly non-exhaustive tests did not confirm that for the
>MSHTML DOM or the MSXML DOM, though. Please elaborate.
>
The DOM Core - getAttribute - method is supposed to always return a
string, [...]
You are correct, but that does not matter as it was already established that
the MSHTML DOM and the MSXML DOM define that method differently ("broken as
designed", if you will).

Therefore, the proof I implicitly asked for is a case where either the
element has the attribute specified and the return value is `null', or where
it does not have the attribute specified and the return value is not `null',
*in the MSHTML 6/7 DOM or the MSXML (3) DOM*.

Thanks for your examples, though. They show that what was defined is close
to what was implemented (the MSDN Library does not mention a Function object
reference as possible return value, for example).


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>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles