6

document.getElementById doesn't seem to work across all browsers (I mean some old ones) and I am sure there are developers who are not aware of this.

What solutions would you suggest to make it cross-browser?

Thanks

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 3
    `document.getElementById` is actually one of the few things that does work across a broad range of browsers, basically anything from the last *several* years, including IE6. What specific browser(s) are you trying to support that don't implement it? – T.J. Crowder Dec 22 '09 at 11:01
  • 3
    Please specify that browser, Thanks – Muhammad Akhtar Dec 22 '09 at 11:02
  • 4
    If document.getElementById isn't supported, don't bother with Javascript for that browser. – kgiannakakis Dec 22 '09 at 11:02
  • IE5 Mac, a colleague told me. And for more c this link plz shared by Vinegar in below answers. http://www.webdeveloper.com/forum/showthread.php?t=172374 With that link, the problem seems to be solved. thanks all – Sarfraz Dec 22 '09 at 11:18
  • 1
    IE5/Mac supported `getElementById()` the last time I used it. Then again, the last time I used it was about five years ago, and I'm a Mac user. In many respects, IE5/Mac had much better DOM support than even IE6/Win ever had. However, it's not really worth supporting it now IMHO, and if you do, you should worry more about CSS compatibility: it implemented CSS2, not the CSS2.1 modern browsers support, and there are some important differences. – NickFitz Dec 22 '09 at 11:59

4 Answers4

9

If document.getElementById doesn't work then either:

  • You're doing it wrong (invalid HTML, trying to access names instead of IDs, etc)

or

  • You're working with Netscape 4.x and Internet Explorer 4.x

There are three ways to deal with browsers of this era.

  • Tell people to upgrade. They are unmaintained, security hole ridden nightmares for user and author alike.
  • Build on stuff that works and make sure your JS checks for the existence of getElementById and friends before trying to use them ( if (!document.getElementById) { return false; /* Insufficient DOM to bother with JS here */ } )
  • Learn about document.all and document.layers
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

Are you sure its not this kind of problem? Have a look its interesting, I didn't know that before.

However, to complement what is already suggested by David Dorward, you write a function like below.

function getElement (id) {

  if (document.getElementById) {
    return document.getElementById(id);
  }

  else if (document.all) {
    return window.document.all[id];
  }

  else if (document.layers) {
    return window.document.layers[id];
  }
} 
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
1
getElemID(obj){

if(document.getElementByID){
 return document.getElementByID(obj);
}

 else if (document.all){
  return document.all[obj];
  }

  else if (document.layers){
     return  document.layers[obj];
     }

  else {
       alert("Could not find support");
       return false;
       }
}
Q_Mlilo
  • 1,729
  • 4
  • 23
  • 26
0
function getDOM() {
    if (document.getElementById) {
        return document.getElementById; 
    }

    var window_document = window.document || {};
    var elements = window_document.all || window_document.layers;
    if(elements) {
        return function(x) { return elements[x]; }
    }

    // everything failed
    throw new InternalError('No means to "getElementById"');
}

... then

var getElementById;
try {
    getElementById = getDOM();
} catch(err) {
    alert(err);
}
// implicit 0K
var oHTMLElement = getElementById('#main');
percebus
  • 799
  • 1
  • 8
  • 21