何时使用 if (document.all&&document.getElementById) 条件

发布于 2024-11-07 13:36:56 字数 125 浏览 3 评论 0原文

想知道何时使用,这适用于什么浏览器?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ低調成傷 2024-11-14 13:36:56

我认为没有任何重要的浏览器不支持 document.getElementById。甚至 IE6 也支持它(尽管它出错了)- 详细信息 ——但它对 document.all 做了同样的事情)。我只使用 document.getElementById 而不是 document.all

如果您使用它只是为了检测浏览器,而不是专门因为您想使用 document.alldocument.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 with document.all). I'd just use document.getElementById rather than document.all.

If you're using this just to detect the browser, rather than specifically because you want to use document.all or document.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).

叶落知秋 2024-11-14 13:36:56

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 using document.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 write document.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.

沉溺在你眼里的海 2024-11-14 13:36:56

这适用于什么浏览器?

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&&document.getElementById)
情况

这种情况的合理用例(不够具体)。
显然,答案是永远

This applies for what browser ?

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…)

When to use if
(document.all&&document.getElementById)
condition

I can't imagine a sensible use case for such a condition (it's not specific enough).
So obviously the answer is never.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文