何时使用 if (document.all&&document.getElementById) 条件
想知道何时使用,这适用于什么浏览器?
if (document.all&&document.getElementById) {
// Some code block
}
Would like to know when to use, This applies for what browser ?
if (document.all&&document.getElementById) {
// Some code block
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有任何重要的浏览器不支持
document.getElementById
。甚至 IE6 也支持它(尽管它出错了)- 详细信息 ——但它对document.all
做了同样的事情)。我只使用document.getElementById
而不是document.all
。如果您使用它只是为了检测浏览器,而不是专门因为您想使用
document.all
或document.getElementById
,浏览器检测通常是一个坏主意。相反,请考虑进行功能检测,测试您想要的实际功能。 此处和此处。离题:如果您希望进行任何重要的跨浏览器工作,您可以考虑使用像 jQuery,原型,YUI、关闭或其他任何一个。它们将为您消除浏览器差异,提供一些非常有用的实用功能,甚至(在某些情况下)修复浏览器错误,例如 IE 对
document.getElementById
的损坏处理(例如 jQuery 就是这样做的,其他一些人也可能如此)。I don't think there's any significant browser left that doesn't support
document.getElementById
. Even IE6 supports it (although it gets it wrong — details — but it does the same thing withdocument.all
). I'd just usedocument.getElementById
rather thandocument.all
.If you're using this just to detect the browser, rather than specifically because you want to use
document.all
ordocument.getElementById
, browser detection is generally a bad idea. Instead, look at doing feature detection, testing for the actual features you want. There are some great examples of feature detection here and here.Off-topic: If you're looking to do any significant cross-browser work, you might consider using a library like jQuery, Prototype, YUI, Closure, or any of several others. They'll smooth over browser differences for you, provide some very useful utility functions, and even (in some cases) fix browser bugs like IE's broken handling of
document.getElementById
(jQuery does that, for instance, some others may as well).document.all
是旧版本 IE(IE3 及更高版本)实现的功能。它也是由 Opera 实现的,试图与仅 IE 的网站兼容。然而,不再建议在任何情况下使用
document.all
——它已经完全过时了。 IE 和 Opera 一直支持它,因为可能有一些旧代码仍在使用它,但在现代代码中没有它的位置。有一些脚本库在 Javascript 中实现了
document.all
版本,以允许旧代码在现代浏览器上运行。因此,甚至不值得使用document.all
作为浏览器测试(正如您在问题中所示)。事实上,要破坏您的测试,我所要做的就是编写document.all = true;
。document.getElememtById
首先由 IE 在 v5.0 中实现,因此您在问题中给出的条件将在 IE5 时代编写,并且将是原始的浏览器检测代码对于 IE5.0 及更高版本,因为这是(当时)唯一支持这两种方法的浏览器。但正如已经讨论过的,它现在几乎毫无用处,而且更有可能给你带来问题,而不是让任何事情发挥作用。document.all
was a feature implemented by old versions of IE (IE3 and higher). It was also implemented by Opera, in an attempt to be compatible with IE-only sites.However
document.all
is no longer recommended for use in any situation -- it is entirely obsolete. IE and Opera have kept support for it because there may be some old code that still uses it, but there is no place for it in modern code.There are some script libraries which implement a version of
document.all
in Javascript, to allow old code to be run on modern browsers. For this reason, it isn't even worth usingdocument.all
as a browser test (as you've shown it in the question). In fact, all I have to do to break your test is writedocument.all = true;
.document.getElememtById
was first implemented by IE in v5.0, so the condition you've given in the question would have been written in the days of IE5, and would have been a primitive browser-detection code for IE5.0 and higher, since that was (at the time) the only browser that supported both methods. But as already discussed, it is pretty much useless now, and is more likely to cause you problems than make anything work.if (document.all)
针对 IE4+、Opera v5(Linux 上的 v4)和 v9.5、Konqueror 2、iCab 2 和 3、Omniweb 4(v4.5 之前)以及一些使用 Rhino javascript 引擎的不起眼的浏览器。if (document.getElementById)
针对任何 DOM1 就绪浏览器(IE5+、NS5+、OP4+ 等...)这种情况的合理用例(不够具体)。
显然,答案是永远。
if (document.all)
targets IE4+, Opera between v5 (v4 on linux) and v9.5, Konqueror 2, iCab 2 and 3, Omniweb 4 (before v4.5) and some obscure browsers using the Rhino javascript engine.if (document.getElementById)
targets any DOM1 ready browsers (IE5+, NS5+, OP4+ etc…)I can't imagine a sensible use case for such a condition (it's not specific enough).
So obviously the answer is never.