getElementById in MSIE

D

dhtml

getElementById doesn't work in IE. IE returns elements that have the
name matching.

So how about replacing getElementById conditionally, after a feature
test.

/**
* XXX: IE Fix for getElementById returning elements by name.
*/
if((function(){
try {
var d = document, x = d.body,
c = d.createElement("<a name=APEgEBITest
style='display:none'>"),
r;
x.insertBefore(x.firstChild);
r = d.getElementById('APEgEBITest');
x.removeChild(c);
return r;
} catch(ex) {}
})())
document.getElementById = function (id) {
var d = document, el = d.getElementById(id), els,i, len;
if(el.id == id) return el;
els = d.getElementsByName(id);
for(i = 0, len = els.length; i < len; i++)
if(els.id === id) return els;
return null;
};

Garrett
 
R

Richard Maher

Hi
So how about replacing getElementById conditionally, after a feature
test.

Or just don't use the ID of one element as the NAME of another element?

Cheers Richard Maher

dhtml said:
getElementById doesn't work in IE. IE returns elements that have the
name matching.

So how about replacing getElementById conditionally, after a feature
test.

/**
* XXX: IE Fix for getElementById returning elements by name.
*/
if((function(){
try {
var d = document, x = d.body,
c = d.createElement("<a name=APEgEBITest
style='display:none'>"),
r;
x.insertBefore(x.firstChild);
r = d.getElementById('APEgEBITest');
x.removeChild(c);
return r;
} catch(ex) {}
})())
document.getElementById = function (id) {
var d = document, el = d.getElementById(id), els,i, len;
if(el.id == id) return el;
els = d.getElementsByName(id);
for(i = 0, len = els.length; i < len; i++)
if(els.id === id) return els;
return null;
};

Garrett
 
D

dhtml

Hi


Or just don't use the ID of one element as the NAME of another element?

That will work. It is better than infinite recursion (example code).

I guess I just expect getElementById and getElementsByName to work
reliably. It is possible to patch them, but at the cost of requiring
the code and feature test to be downloaded.

Minified, the following code is 423 bytes and that's mostly in the
feature test. If I could reduce more, it might be more worth it.

/**
* XXX: IE Fix for getElementById returning elements by name.
*/
(function(){
var d = document, x = d.body, c,
g = 'getElementById',
orig = document[g];

if(!x) return setTimeout(arguments.callee,50);

try {
c = d.createElement("<A NAME=0>");
x.insertBefore(c, x.firstChild);
if(d[g]('0')){
x.removeChild(c);
d[g] = getElementById;
}
} catch(x){}
function getElementById(id) {
var el = Function.prototype.call.call(orig, this, id), els, i;

if(el.id == id) return el;
els = this.getElementsByName(id);

for(i = 0; i < els.length; i++)
if(els.id === id) return els;
return null;
};
})();


Garrett
 
G

Gregor Kofler

dhtml meinte:
getElementById doesn't work in IE. IE returns elements that have the
name matching.

Thats an old, well known issue.
So how about replacing getElementById conditionally, after a feature
test.

Seems to be a solution without the problem. Since web (and JS) authors
should be aware of this issue, its easily avoided by giving ids that
differ from names.
/**
* XXX: IE Fix for getElementById returning elements by name.
*/

Since gEbId() is - supposedly (never testet, since I hardly ever use or
need it) - a rather slowishy way to access elements, the added overhead
makes it even more so.

Gregor
 
D

David Mark

getElementById doesn't work in IE. IE returns elements that have the
name matching.

So how about replacing getElementById conditionally, after a feature
test.

/**
 * XXX: IE Fix for getElementById returning elements by name.
 */
if((function(){
  try {
    var d = document, x = d.body,
        c = d.createElement("<a name=APEgEBITest
style='display:none'>"),
        r;
    x.insertBefore(x.firstChild);
    r = d.getElementById('APEgEBITest');
    x.removeChild(c);
      return r;
  } catch(ex) {}})())

document.getElementById = function (id) {

I wouldn't do that. But if you really need a general solution for
this, you could use a wrapper with a one-off test. The branch for
agents that pass the test would be one line. As for IE, if the the ID
of the element returned by IE's bungled gEBI method does not match the
ID passed to your wrapper, then return null, which should break the
calling app, which is what should happen (then the developer will see
the error and fix their broken markup.)
 

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
474,002
Messages
2,570,258
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top