javascript - How to run a function when any XMLHttpRequest is complete? -
i'm working on project has several scripts cannot change. these scripts update page via ajax. when update complete need run code.
is there event fires when xmlhttprequest complete? (or xmlhttprequest state change?).
unfortunately cannot access specific xmlhttprequest object used make request.
thanks,
without jquery, can hook open
method attach listener each xhr object's readystatechange
events @ time xhr object open
ed. ensure following code runs before ajax occurs:
// save real open var oldopen = xmlhttprequest.prototype.open; function onstatechange(event) { // fires on every readystatechange ever // use `this` determine xhr object fired change event } xmlhttprequest.prototype.open = function() { // when xhr object opened, add listener readystatechange events this.addeventlistener("readystatechange", onstatechange) // run real `open` oldopen.apply(this, arguments); }
alternatively, if care successful load
events, can listener event instead of readystatechange
.
Comments
Post a Comment