How can I call a JavaScript function after every a4j AJAX response?

后端 未结 4 571
遇见更好的自我
遇见更好的自我 2021-01-14 02:45

I am working on a web app using JSF w/Seam. I want to be able to call a JavaScript function after every ajax response. I\'m looking for a way to do this without putting an o

4条回答
  •  太阳男子
    2021-01-14 03:06

    Rewritten answer: see original answer in revision history

    You could override the default send method of XMLHttpRequest with one that hijacks the readystatechange handler:

    (function () 
    { 
        var xhrSend = XMLHttpRequest.prototype.send; 
        XMLHttpRequest.prototype.send = function  () 
         { 
            var handler = this.onreadystatechange; 
            this.onreadystatechange = function () 
            { 
                if (handler) {
                    if (handler.handleEvent) handler.handleEvent.apply(xhr, arguments);
                    else handler.apply(xhr, arguments);
                }
                if (this.readyState == 4) 
                { 
                    // your oncomplete function here 
                    this.onreadystatechange = handler; 
                 } 
             }; 
            xhrSend.apply(this, arguments); 
        }; 
    })(); 
    

    Edit: The above function doesn't work with jQuery requests, and so potentially it could fail with other libraries as well. The revision below addresses the issue with a setTimeout hack to delay the code that overrides the handler. Of course, with jQuery, you can just use the .ajaxSuccess() global handler, but for other libraries with similar behavior, this would be useful.

    (function() {
        function globalHandler() {
            if (this.readyState == 4) {
                // your oncomplete code here
            }
        }
        var xhrSend = XMLHttpRequest.prototype.send;
        XMLHttpRequest.prototype.send = function() {
            var xhr = this;
            if (xhr.addEventListener) {
                xhr.removeEventListener("readystatechange", globalHandler);
                xhr.addEventListener("readystatechange", globalHandler, false);
            }
            else {
                function readyStateChange() {
                    if (handler) {
                        if (handler.handleEvent)
                            handler.handleEvent.apply(xhr, arguments);
                        else
                            handler.apply(xhr, arguments);
                    }
                    globalHandler.apply(xhr, arguments);
                    setReadyStateChange();
                }
                function setReadyStateChange() {
                    setTimeout(function() {
                        if (xhr.onreadystatechange != readyStateChange) {
                            handler = xhr.onreadystatechange;
                            xhr.onreadystatechange = readyStateChange;
                        }
                    }, 1);
                }
                var handler;
                setReadyStateChange();
            }
            xhrSend.apply(xhr, arguments);
        };
    })();
    

    http://jsfiddle.net/gilly3/FuacA/5/
    I tested this in IE7-9, and the latest versions of Chrome and FF

提交回复
热议问题