Javascript-跟踪任何xmlhttprequest [英] Javascript - Track any xmlhttprequest
问题描述
我可以检查页面上的任何XmlHttpRequest可执行文件,而无需添加每个组件的addEventListener吗?我尝试添加
Can I check any XmlHttpRequest, executable on page without adding addEventListener each of it? I try add
document.addEventListener("loadend"...)
但是什么也没发生.看起来请求没有全局事件?
but nothing happend. Looks like requests don't have a global events?
//编辑//
我需要检测ajax加载的新元素.很棒的解决方案-在这里
I need detect new elements, which loaded by ajax. Great solution for it- here
推荐答案
我认为您应该重新考虑您的设计.你为什么要这样做.有更好的选择吗?
I think you should rethink your design. Why do you want to do this. Are there better options?
但是,如果您确实要执行此操作,则可以通过用您自己的对象覆盖标准的 XmlHttpRequest
对象来对其进行破解".尽管必须启动任何 XmlHttpRequest
,才能使它适用于所有对象.
However if you really want to do this you could "hack" it by overwriting the standard XmlHttpRequest
object with your own. You will have to do this before any XmlHttpRequest
is initiated though to make it work for all objects.
//store the original XMLHttpRequest in a variabele so you can use it yourself
var originalXMLHttpRequest = XMLHttpRequest;
//Overwrite the original object with your own in which you create an instance of the original object, add an event listner to it and return that.
XMLHttpRequest = function() {
var req = new originalXMLHttpRequest();
req.addEventListener("loadend", function() {
console.log("loadend event fired");
});
return req;
};
//Test if it works
httpReq = new XMLHttpRequest();
httpReq.open("GET", "https://stackoverflow.com/");
httpReq.send();
这篇关于Javascript-跟踪任何xmlhttprequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!