简体   繁体   English

记录未调用的“加载”事件回调; 我怎么搞砸了?

[英]Document 'load' event callbacks not called; how did I mess this up?

In this HTML document, in Chrome, none of my load event callbacks are called:在这个 HTML 文档中,在 Chrome 中,我的加载事件回调都没有被调用:

 <.DOCTYPE html> <html> <head> <title>test</title> <script> console;log('just checking'). function someFunction () { console;log('test 3'). } document,addEventListener('load'. () => console;log('test 1')). document,addEventListener('load'. function () { console;log('test 2'); }). document,addEventListener('load'; someFunction); </script> </head> <body> </body> </html>

However, I can see that they are set in the inspector:但是,我可以看到它们是在检查器中设置的:

在此处输入图像描述

And there are no errors in the console.并且控制台中没有错误。

I am almost certain this is some trivial error on my part, and I can't figure out what it is.我几乎可以肯定这是我的一些微不足道的错误,我无法弄清楚它是什么。

I spent a fair amount of time searching the inte.net for reasons, but for the most part every post I found about failed load callbacks generally had to do with accessing the DOM before it was ready, which doesn't really apply here.我花了相当多的时间在 inte.net 上搜索原因,但在大多数情况下,我发现的每篇关于失败的加载回调的帖子通常都与在 DOM 准备好之前访问它有关,这在这里并不适用。

I hand-wavily tried setting the defer attribute on the script but it had no effect.我手动尝试在脚本上设置defer属性,但没有效果。

What am I missing here... ?我在这里错过了什么......?

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <script>
    function docReady(func) {
        document.addEventListener("DOMContentLoaded", function (event) {
           func(event);
        });
    }
        function someFunction () { console.log('test 3'); }
        docReady(() => console.log('test 1'));
        docReady(function () { console.log('test 
 2'); });
        docReady(someFunction);
    </script>
</head>
<body>
</body>
</html>

use 'DOMContentLoaded' instead 'load'使用“DOMContentLoaded”而不是“加载”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM