IE7 and getElementsByName()
-
Tuesday, July 10, 2007 9:33 PM
When will Microsoft comply with W3C Dom methods?
It is a simple question, can anyone supply an answer?
I have the following code:
Code Snippetvar nodes = document.getElementsByName('FeedSiteItem');
Now, in FF it corectly returns an array of 3 itens. But in IE7 it returns nothing. A big fat old zero length array to be exact.
Now, if that piece of code is not super-simple I really do not know what is.
Do Microsoft not think it is time for some compliancy here? What is so wrong with standards? Are they scared of conforming?
Why must we continue to write 2 versions of our code, or constantly have to find workarounds?
I swore I would never turn away from MS products, since I have used them since the advent of DOS, but I don't know how much more I can take.
Can someone please give me a straight answer as to why that code snippet will work sometimes and not others?
Come on Microsoft, you wrote the bloody software, tell me why it doesn't work!
All Replies
-
Friday, August 17, 2007 3:18 PM
Use this. Example -
var nodes = getElementsByName_iefix(tag, "FeedSiteItem")
You will be about to get a length count also.
function getElementsByName_iefix(tag, name) {
var elem = document.getElementsByTagName(tag);
var arr = new Array();
for(i = 0,iarr = 0; i < elem.length; i++) {
att = elem[i].getAttribute("name");
if(att == name) {
arr[iarr] = elem[i];
iarr++;
}
}
return arr;
}- Proposed As Answer by Jayvardhan Patil Saturday, April 24, 2010 2:02 PM
-
Friday, August 17, 2007 5:56 PMI might suggest an alteration to this...
1.) for IE, just reset the function to the correct behavior (override it)
2.) if you want the tag name fuctionality, add it as a second parameter, so it doesn't go against the existing spec...
3.) you don't need a counter on the array, just use .push( item ) to append it
(Note: if IE does plan to follow the spec *exactly* at some point later, you'll need to not pass the tag param, that said, officially I believe only form elements are "legally" allowed to contain the name attribute, so the collection inside here could be specifically targeting only the input,select, textarea,button, form elements instead if desired.)
Code Snippetif(typeof(window.external) != 'undefined'){
//yes, this is evil browser sniffing, but only IE has this bug
document.getElementsByName = function(name, tag){
if(!tag){
tag = '*';
}
var elems = document.getElementsByTagName(tag);
var res = []
for(var i=0;i<elems.length;i++){
att = elems[i].getAttribute('name');
if(att == name) {
res.push(elems[i]);
}
}
return res;
}}
-
Friday, August 31, 2007 7:38 PMWhat tag do the elements you're looking for have?
I'm a firefox user, and almos never user IE, but I had the same problem recently. After some searching, I found this site http://www.toolazy.me.uk/template.php?content=getelementsbyname.xml which says IE is actually following the specification:
(From the linked site)
" Its supported by both Internet Explorer and Firefox since it is part of the DOM Level 1 specification. Unfortunately theres a problem. From the Html 4.01 Spec, only the A, APPLET, BUTTON, FORM, FRAME, IFRAME, IMG, INPUT, OBJECT, MAP, META, PARAM, TEXTAREA and SELECT are supported which means that you can't just name all your divs, spans or rows with the same name and use getElementsByName() to return an array of items."
So it seems firefox is the one who's not complying to standards. I'm gonna try the function posted here, hoping it works without having to change a lot of my code. -
Tuesday, September 04, 2007 6:32 PMFirefox may let you assign a name attribute on elements that it shouldn't, but that is a far cry better than IE, which won't return you the items you legitimately, legally assigned them to in the first place. (e.g. on an input, or select element)
What is most frustrating, is that the IE developers didn't seem to understand the need for .getElementsByName() and didn't implement it, but also decided that they would botch the .getElementById() method with a hack, to return the first element matching by name also.
keep in mind, just because in firefox you can assign a name to a non-in-the-list element, doesn't mean that you should, or would. I wouldn't recomend assigning a name to any other element, but when you want all elements with the name "userchoices" (say a set of checkboxes), not being able to call .getElementsByName() in IE is a royal PITA.
cheers -
Tuesday, September 04, 2007 9:51 PM
easy fix written in VBA. I simplified it from a web source, I don't remember the source though.
Code SnippetFunction getElementsById(sId)
Set outArray = CreateObject("Scripting.Dictionary")
Set aEl = document.getElementsByTagName("*")
For Each m_aEl In aEl
If m_aEl.id = sId then outArray.Add outArray.Count+1, m_aEl
Next
Set getElementsById = outArray
End Function -
Thursday, October 07, 2010 4:01 PMsokoii

