473,569 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('in_te st');
alert(intest.ge tAttribute('typ e') + " " +
intest.attribut es['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
Sep 4 '08 #1
5 3968
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.getEle mentById('in_te st');
alert(intest.ge tAttribute('typ e') + " " +
intest.attribut es['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.or g/>
...

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>
Sep 4 '08 #2
On Sep 4, 6:29*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
wrote:
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.getEle mentById('in_te st');
* * alert(intest.ge tAttribute('typ e') + " " +
intest.attribut es['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.or g/>
...
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.
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.
Sep 5 '08 #3
David Mark wrote:
[...] 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.
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, <f8************ *******@news.de mon.co.uk>
Sep 7 '08 #4
Thomas 'PointedEars' Lahn wrote:
David Mark wrote:
>[...] 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, yet with:-

<html>
<head>
<title></title>
<script type+"text/javascript">
window.onload = function(){
var el = document.getEle mentById('test' );
alert('onclick = '+(typeof el.getAttribute ('onclick')));
}
</script>
</head>
<body>
<input type="button"
value="Test"
onclick="alert( 'intrinsic event code');"
id="test"
>
</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.getEle mentById('test' );
alert('checked = '+(typeof el.getAttribute ('checked')));
}
</script>
</head>
<body>
<input type="checkbox"
checked
id="test"
>
</body>
</html>

- shows 'boolean' for the checked attribute.

Richard.

Sep 8 '08 #5
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
>David Mark wrote:
>>[...] 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>
Sep 8 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7226
by: John | last post by:
Is there an equivalent of COM on Linux that I can get through Python. My need is to have some sort of language independent component framework. I can think of CORBA but I have to have a server running. I prefer not to. I just need Python components for local consumption in other languages. I remember Gnome libs having some thing like this....
2
3271
by: Michael Foord | last post by:
Please pardon my ignorance on this one - but I'm not certain how the sign bt is treated in python bitwise operators. I've trying to convert a javascript DES encryption routine into python. Javascritp has >>> and >>. >>> is a zero fill bit shift whereas >> is a sign propagating bit shift. My understanding is that the python >> is equivalent...
3
2281
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an ordinary Lisp S-expression. So it's easy enough to translate some XML into equivalent Lisp. Now I turn it over to the Lisp parser, which creates the...
1
3779
by: Vannela | last post by:
Is there any equivalent control in .NET for the Power builder DataWindow control? I am explaining the Datawindow architecture to some extent. Power Builder DataWindow Control has got different presentation styles and different data sources. Presentation styles like tabular format , graph format, grid format, freeform format, Composite...
6
5153
by: Frank Rachel | last post by:
So I can focus on the correct areas of research, I was wondering if someone could give me the .NET equivelents for this J2EE architecture: JSP's make calls to local JavaBean Controls. The controls do a JNDI lookup to invoke methods on EJB's. The EJB's use local Java classes, and these classes use JDBC to do database work. Example: ...
7
3717
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s : String;
10
7310
by: karch | last post by:
How would this C# contruct be represented in C++/CLI? Or is it even possible? PolicyLevel level = (enumerator->Current) as PolicyLevel; Thanks, Karch
9
4032
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric equivalent. Is there such a function available in C#? I can see that VB.NET has one, but I couldn't see how to get at it in C#. For example, if I have...
14
2523
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if both statements are similar : *((char **)v)++ == *((char **)v++) Where v is a pointer to an array of characters,defined as char *v;
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.