最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS: How to make document.getElementById cross-browser? - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question edited Dec 22, 2009 at 11:15 Adeel Ansari 39.9k12 gold badges96 silver badges135 bronze badges asked Dec 22, 2009 at 10:57 SarfrazSarfraz 383k82 gold badges559 silver badges612 bronze badges 5
  • 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 Commented Dec 22, 2009 at 11:01
  • 3 Please specify that browser, Thanks – Muhammad Akhtar Commented Dec 22, 2009 at 11:02
  • 4 If document.getElementById isn't supported, don't bother with Javascript for that browser. – kgiannakakis Commented Dec 22, 2009 at 11:02
  • IE5 Mac, a colleague told me. And for more c this link plz shared by Vinegar in below answers. webdeveloper./forum/showthread.php?t=172374 With that link, the problem seems to be solved. thanks all – Sarfraz Commented Dec 22, 2009 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 patibility: it implemented CSS2, not the CSS2.1 modern browsers support, and there are some important differences. – NickFitz Commented Dec 22, 2009 at 11:59
Add a ment  | 

4 Answers 4

Reset to default 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

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

However, to plement 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];
  }
} 
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;
       }
}
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');
发布评论

评论列表(0)

  1. 暂无评论