"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bd*******************@news.demon.co.uk...
<snip>
... That is 120 odd lines of code to provide a very limited
emulation of getElementsByTagName on a browser that is
little used and will probably not let you do whatever you were
planning on doing with the elements anyway.
You might still be interested in what that 120 odd lines of code might
look like. This is an example, its strategy is to normalise
document.getElementsByTagName on browser that do not support it. This
version does not attempt to handle the problem of extracting CSS
positioned elements on Netscape 4.
if(!document.getElementsByTagName){
if(document.all){
document.getElementsByTagName = function(nem){
return document.all.tags(nem.toUpperCase());
}
}else{
document.getElementsByTagName = function(){
var selTypes = ['select-one','select-multiple','select'];
var inpTypes = ['radio','text','reset','submit','image',
'password','hidden','checkbox','button','file'];
var txATypes = ['textarea'];
function handleName(name, ob, ar){
if((ar[name])&&(ar[name] != ob)){
if((typeof ar[name].length == 'number')&&
(!ar[name].options)){ //is an array (not a select)
var add = true;
for(var c = ar[name].length;c--;){
if(ar[name][c] == ob){
add = false;
break;
}
}
if(add)ar[name][ar[name].length] = ob;
}else{ //is an element
ar[name] = [ar[name], ob];
}
}else{
ar[name] = ob;
}
}
function addCollection(col, ar){
if(col){
for(var ob,c = col.length;c--;){
ob = col[c];
if(ob.name)handleName(ob.name, ob, ar);
if(ob.id)handleName(ob.id, ob, ar);
ar[ar.length] = ob;
}
}
};
function addOptionsEls(frms, ar){
var ob,sels = [];
addFormEls(frms, 'select', sels);
for(var c = sels.length;c--;){
if(sels[c].options){
for(var d = sels[c].options.length;d--;){
ob = sels[c].options[d];
if(ob.name)handleName(ob.name, ob, ar);
if(ob.id)handleName(ob.id, ob, ar);
ar[ar.length] = ob;
}
}
}
}
function addFormEls(frm, nem, ar){
if(frm){
var types;
switch(nem){
case 'input':
types = inpTypes;
break;
case 'select':
types = selTypes;
break;
case 'textarea':
types = txATypes;
break;
default:
break;
}
if(types){
for(var e = frm.length;e--;){
var fr = frm[e].elements||frm[e];
var ob,ty,el;
for(var c = fr.length;c--;){
el = fr[c];
if((typeof el.length == 'undefined')||(el.options)){
el = [el];
}
for(var f = el.length;f--;){
if(ty = el[f].type){
for(var d = types.length;d--;){
if(ty == types[d]){
ob = el[f];
if(ob.name)handleName(ob.name, ob, ar);
if(ob.id)handleName(ob.id, ob, ar);
ar[ar.length] = ob;
break;
}
}
}
}
}
}
}
}
};
function searchDoc(doc, nem, ar){
if(doc){
switch(nem){
case 'a':
addCollection(doc.links, ar);
addCollection(doc.anchors, ar);
break;
case 'img':
addCollection(doc.images, ar);
break;
case 'input':
case 'select':
case 'textarea':
addFormEls(doc.forms, nem, ar);
break;
case 'option':
addOptionsEls(doc.forms, ar);
break;
case 'form':
addCollection(doc.forms, ar);
break;
default:
break;
}
if(doc.layers){
for(var c = doc.layers.length;c--;){
searchDoc(doc.layers[c].document, nem, ar);
}
}
}
};
return function(nem){
var ar = [];
searchDoc(document, nem.toLowerCase(), ar)
return ar;
};
}();
}
}
Richard.
--
Example JavaScript DOM listings for: Opera 7.11,
Mozilla 1.2 and ICEbrowser 5.4
<URL:
http://www.litotes.demon.co.uk/dom_root.html >