监控浏览器控制台中的所有JavaScript事件 [英] Monitor all JavaScript events in the browser console

查看:1073
本文介绍了监控浏览器控制台中的所有JavaScript事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以听所有的JavaScript事件吗?

Is it possible to listen to all javascript events?

我试图猜测在AJAX请求修改DOM后是否触发了一个事件。 / p>

I'm trying to guess if there's an event triggered after the DOM is modified by an AJAX request.

推荐答案

使用 firebug web inspector ,您可以使用 monitorEvents

monitorEvents(myDomElem);

这将打印由 myDomElem 发出的所有事件控制台。使用 unmonitorEvents 来停止监视事件。

This prints all events emitted by myDomElem to the console. Use unmonitorEvents to stop monitoring events.

如果您有兴趣在DOM被操纵之后获取事件,请查看突变事件

If you're interested in getting events after the DOM has been manipulated, take a look at Mutation Events.

修改

据我所知,没有从所有XMLHttpRequest中截取所有 onreadystatechange 事件的简单方法。我可以想到的唯一的解决方法是用你自己的实现来覆盖本机的XMLHttpRequest对象。例如:

As far as I know, there is no easy way to intercept all onreadystatechange events from all XMLHttpRequest. The only work-around I can think of is to override the native XMLHttpRequest object with you own implementation. For example:

(function() { // Overriding XMLHttpRequest
    var oldXHR = window.XMLHttpRequest;

    function newXHR() {
        var realXHR = new oldXHR();

        realXHR.addEventListener("readystatechange", function() { 
            console.log("an ajax request was made") 
        }, false);

        return realXHR;
    }

    window.XMLHttpRequest = newXHR;
})();

不用说这是非常黑客,通常不太了解。

Needless to say this is extremely hacky and generally ill-advised.

这篇关于监控浏览器控制台中的所有JavaScript事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆