如何在没有侦听器的情况下检查 DOM 是否已准备好?

发布于 2024-12-11 06:20:30 字数 137 浏览 2 评论 0原文

如果我有一个动态加载的脚本,我希望它等到 DOM 准备好后再执行代码。但是,如果脚本加载太慢,则 DOM 已经准备好,因此 DOM-Ready 函数将不会运行。

请不要使用任何框架,我依赖于纯 JavaScript。

提前致谢!

If I have a script that is loaded dynamically, I want it to wait until the DOM is ready before executing code. However, if the script loads too slowly, the DOM will already be ready and therefore the DOM-Ready function will not run.

No frameworks, please, I'm relying on pure JavaScript.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

微暖i 2024-12-18 06:20:30

如果没有监听器,就没有 100% 可靠的方法来确保加载整个 DOM。您可以执行以下操作:

var myChecker = setInterval(function () {
  var checkElem = document.getElementById('myRefElement');

  if (checkElem != null) {
    clearInterval(myChecker);
    myFunction();
  }
}, 100);

这将等到您关心的某个目标元素存在。

Without a listener there's no 100% reliable way to ensure that the entire DOM is loaded. You can do something like this:

var myChecker = setInterval(function () {
  var checkElem = document.getElementById('myRefElement');

  if (checkElem != null) {
    clearInterval(myChecker);
    myFunction();
  }
}, 100);

That'll wait until some target element you care about exists.

够运 2024-12-18 06:20:30

此页面的一部分: http://dean.edwards.name/weblog/2006 /06/again/ 你会发现这段代码,这就是我用来做你所问的事情的代码:

我用代码留下了评论,因为我没有写它:

// Dean Edwards/Matthias Miller/John Resig

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;

Part way down on this page: http://dean.edwards.name/weblog/2006/06/again/ you will find this code, which is what I use to do what you are asking about:

I leave the comment with the code as I didn't write it:

// Dean Edwards/Matthias Miller/John Resig

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;
云淡月浅 2024-12-18 06:20:30

非常简单 - 将脚本放在紧邻结束正文标记之前(如果有的话)。它不保证 DOM 已准备好,但它比 DOM 就绪侦听器更可靠,并且比加载侦听器更早运行。

Very simple - put your script immediately before the closing body tag (if you have one). It doesn't guarantee that the DOM is ready, but it's more reliable that DOM ready listeners and runs earlier than load listeners.

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