Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果在设置侦听器之前触发了DOMContentLoaded事件,如何检索“event”对象_Javascript_Events - Fatal编程技术网

Javascript 如果在设置侦听器之前触发了DOMContentLoaded事件,如何检索“event”对象

Javascript 如果在设置侦听器之前触发了DOMContentLoaded事件,如何检索“event”对象,javascript,events,Javascript,Events,是否有方法检索DOMContentLoaded事件的事件对象,即使该对象在设置eventListener之前已触发 我找到了一些DOMContentLoaded的计时数据,希望事件数据也能被存储 window.performance.timing.domContentLoadedEventStart window.performance.timing.domContentLoadedEventEnd 我想将事件对象传递给我的回调,不管它是直接调用的还是作为eventlistener的结果调用的

是否有方法检索
DOMContentLoaded
事件的
事件
对象,即使该对象在设置eventListener之前已触发


我找到了一些
DOMContentLoaded
的计时数据,希望事件数据也能被存储

window.performance.timing.domContentLoadedEventStart
window.performance.timing.domContentLoadedEventEnd
我想将
事件
对象传递给我的回调,不管它是直接调用的还是作为eventlistener的结果调用的

var callback = function(event){
    console.log(event)
}
if (document.readyState !== "loading") {
    var event = window.DOMContentLoadedEvent; // doesnt exist
    callback.call(this, event);
} else {
    document.addEventListener('DOMContentLoaded', callback, false)
}
我想我可以创建一个新对象并返回它,但我希望我的代码尽可能小

var event = {
    srcElement: document,
    target:document,
    timeStamp:window.performance.timing.domContentLoadedEventEnd,
    type:"DOMContentLoaded",
}
callback.call(this, event);

我所做的是添加另一个事件监听器,它肯定会在
DOMContentLoaded
事件之前注册。如果触发,处理程序将存储
事件
对象

document.addEventListener('DOMContentLoaded', function(event){
    window.DOMContentLoadedEvent = event; 
});
编辑 我的代码是一个非常简单的domready函数

document.addEventListener('DOMContentLoaded', function(event){
    window.DOMContentLoadedEvent = event; 
});
domready = function(callback) {

    if (document.readyState === "loading") {
      return document.addEventListener('DOMContentLoaded', callback, false);
    }
    return callback.call(this, window.DOMContentLoadedEvent);
}

以下代码可能驻留在外部脚本中,并在触发
DOMContentLoaded
事件后插入

domready(function(event){
    console.log(event)
});
我是否能够在不设置其他事件侦听器的情况下检索
事件
数据?

检查此项:

<body>
...
<script>

    var DOMevent; // global variable

    document.addEventListener('DOMContentLoaded', function(event) {
        DOMevent = event; // save it
    }, false);

    window.addEventListener('load', function () {
        processEvent(DOMevent);  // works fine
    }, false);

    function processEvent(e) {
        console.log(e);
    }
</script>
</body>

...
var DOMevent;//全局变量
document.addEventListener('DOMContentLoaded',函数(事件){
DOMevent=event;//保存它
},假);
window.addEventListener('load',函数(){
processEvent(DOMevent);//工作正常
},假);
函数processEvent(e){
控制台日志(e);
}
console.log(e)将显示
DOMevent

有关readyState的更多信息


有关
DOMContentLoaded
事件的详细信息

有关“加载”事件的详细信息


*示例中的输入错误<代码>文档
->
窗口

window.performance.timing.domContentLoadedEventStart
window.performance.timing.domContentLoadedEventEnd
是时间戳。不确定您想要实现什么?是的,由于存储了domContentLoaded时间戳,我希望其他一些数据可能隐藏在窗口对象的某个地方。您想要检索什么数据?您能否使用单个
文档。addEventListener(“DOMContentLoaded”)
?事件最多调用一次,是吗?是,事件只调用一次,但可能有多个处理程序。由于回调会有所不同,我希望无论是否使用is,都可以使用回调可用的完整事件对象。
DOMContentLoaded
是第一个在浏览器中触发的事件(我说得对吗?),它只有在处理完所有
后才会触发(!!!而且它们不是异步的或差异的!)刚刚意识到我使用了错误的readystate,谢谢你的链接。