Microsoft Madness while writting Web2 App !!

J

JT

arghh.. I am writing a web app for real-time streaming of market data
(equities) and am implementing a quick getElementsById function as
needed; in gecko FF this is a walk with document.evaluate. In IE i am
using document.all (i presume this is the only efficient alternative
for IE, am i correct?)..

Anyway the madness is this:

var _sID = "501"; // some arbitrary ID which exists in the hierarchy.

var result1 = (typeof document.all[_sID]); //result1 = undefined

// However

for (var x in document.all)
{
if(x == _sID ) //this case is eventually hit (i.e equals true at a
given point)
result2 = document.all[x]; // result2 = is also undefined!!!!
}

Now why would IE say that result2 is undefined when that value was
returned in the enumeration.

Some debugging reveals that this problem only occurs with pure
numerical IDs!!! So pre-pending any market code with an arbitrary
string solves the problem!!! Truly i would love to know what the IE
team was thinking when they implemented this collection!!!

Any thoughts on this? Thank you.
 
A

Andy Dingley

Some debugging reveals that this problem only occurs with pure
numerical IDs!!!

If it doesn't begin with a letter (not a digit), then it can't be a
well-formed NAME or ID token, thus isn't well-formed to use in an
attribute (such as id) that is declared as being of type ID. If you
validated your HTML, you'd see this.
<http://www.w3.org/TR/html4/types.html#type-id>

This rarely causes a problem for the display of HTML, however it does
cause trouble (as you've seen) with getElementById() etc.
 
J

Jonathan N. Little

JT said:
arghh.. I am writing a web app for real-time streaming of market data
(equities) and am implementing a quick getElementsById function as
needed; in gecko FF this is a walk with document.evaluate. In IE i am
using document.all (i presume this is the only efficient alternative
for IE, am i correct?)..

Anyway the madness is this:

var _sID = "501"; // some arbitrary ID which exists in the hierarchy.

Some debugging reveals that this problem only occurs with pure
numerical IDs!!! So pre-pending any market code with an arbitrary
string solves the problem!!! Truly i would love to know what the IE
team was thinking when they implemented this collection!!!

Your debugging pointed you to your problem, but in this case it not IE's
fault, but the other way around

'ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").'

http:///www.w3.org/TR/html4/types.html#type-name

"5" is not a letter
 
B

Bergamot

JT said:
am implementing a quick getElementsById function

I assume you mean getElementById (singular, not plural)
In IE i am
using document.all (i presume this is the only efficient alternative
for IE, am i correct?)..

getElementById works in IE just fine. Have you tried it?
 
J

JT

If it doesn't begin with a letter (not a digit), then it can't be a
well-formed NAME or ID token, thus isn't well-formed to use in an
attribute (such as id) that is declared as being of type ID. If you
validated your HTML, you'd see this.
<http://www.w3.org/TR/html4/types.html#type-id>

This rarely causes a problem for the display of HTML, however it does
cause trouble (as you've seen) with getElementById() etc.

OK i didn't realise that it was not good design to use pure numerical
values for ID. Thanks for pointing it out. I guess we seam to start
blaming microsoft for any problems we have, not good.. cheers.
 
J

JT

I assume you mean getElementById (singular, not plural)


getElementById works in IE just fine. Have you tried it?

Nope i did mean getElementsById, getElementById only returns one
object irrespective of how many objects with the same ID exist. In
this case i may have the same market data appear in different DIV
elements and this just want to return an array of all Divs with that
ID; something getElementById will not do. cheers.
 
B

Bergamot

Neredbojias said:
Say what? There ain't no "getElementsById", bro.

Um, he just explained he's writing his own function to get multiple
elements.
Some say there _is_ a
"getElementsByName"

Maybe you mean getElementsByTagName, which does work beautifully.
 
J

Jeremy J Starcher

Nope i did mean getElementsById, getElementById only returns one object
irrespective of how many objects with the same ID exist. In this case i
may have the same market data appear in different DIV elements and this
just want to return an array of all Divs with that ID; something
getElementById will not do. cheers.

HTML 4.01 strict requires that element ids be unique within the document,
though most (all?) browsers do not enforce that rule. Hence, there is no
get ElementsByID.

One common way to flag multiple elements is to give them multiple
classes. Then you can cycle through the elements you are after, see if
they belong to a certain class, then do your operations.

Sample code: (I didn't feel like tracking down the URL)

This code finds all tables in my document that have the class
'ruler' or 'tech' and sets up the table rows for mouse-over highlighting.


/*
tableruler()
written by Chris Heilmann for alistapart.
enables a rollover of rows for each table with the classname ...
*/

function tableruler()
{
if (document.getElementById && document.createTextNode)
{
var tables=document.getElementsByTagName('table');
for (var i=0;i<tables.length;i++)
{
if ((tables.className=='ruler') || (tables.className=='tech'))
{
var trs=tables.getElementsByTagName('tr');
for(var j=0;j<trs.length;j++)
{
if(trs[j].parentNode.nodeName=='TBODY' && trs[j].className=="")
{
trs[j].onmouseover=function(){this.className='ruled';return
false}
trs[j].onmouseout=function(){this.className='';return false}
}
}
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top