简体   繁体   English

每个a4j AJAX响应后如何调用JavaScript函数?

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

I am working on a web app using JSF w/Seam. 我正在使用带有Seam的JSF的Web应用程序。 I want to be able to call a JavaScript function after every ajax response. 我希望能够在每个ajax响应后调用JavaScript函数。 I'm looking for a way to do this without putting an oncomplete attribute on every commandLink/commandButton on every page. 我正在寻找一种无需在每个页面的每个commandLink / commandButton上放置oncomplete属性的方法。

I think there's a way to set up a servlet filter (interceptor? I get the terms confused) to inject the JS call into each response. 我认为有一种方法可以设置一个servlet过滤器(拦截器?让我感到困惑),将JS调用注入每个响应中。 I'm going to look into that. 我将对此进行调查。 In the meantime, if anyone has any other suggestions, I'm all ears. 在此期间,如果有人有其他建议,我会非常高兴。

EDIT: I think the jQuery ajaxSuccess method might be the way to go here, but I'm not sure how to actually use it. 编辑:我认为jQuery ajaxSuccess方法可能是这里的方法,但是我不确定如何实际使用它。 I can't get anything to register. 我什么都不能注册。 I basically want to add code to get any and all ajax requests from any source to call my JavaScript method on success. 我基本上想添加代码以从任何来源获取任何和所有ajax请求,以在成功时调用我的JavaScript方法。 Can anyone show me the proper way to do this? 谁能告诉我正确的方法? I've tried a number of ways to do this, including adding jQuery("*").ajaxSuccess(function(){myFunction();}); 我尝试了多种方法来做到这一点,包括添加jQuery("*").ajaxSuccess(function(){myFunction();}); to the bottom of my template xhtml file. 到我的模板xhtml文件的底部。

Rewritten answer: see original answer in revision history 重写答案: 在修订历史记录中查看原始答案

You could override the default send method of XMLHttpRequest with one that hijacks the readystatechange handler: 您可以使用劫持readystatechange处理程序的方法重写XMLHttpRequest的默认send方法:

(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. 编辑:上面的功能不适用于jQuery请求,因此可能会因其他库而失败。 The revision below addresses the issue with a setTimeout hack to delay the code that overrides the handler. 下面的修订版通过setTimeout hack解决了该问题,以延迟覆盖处理程序的代码。 Of course, with jQuery, you can just use the .ajaxSuccess() global handler, but for other libraries with similar behavior, this would be useful. 当然,使用jQuery,您可以只使用.ajaxSuccess()全局处理程序,但是对于其他具有类似行为的库,这将很有用。

(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/ http://jsfiddle.net/gilly3/FuacA/5/
I tested this in IE7-9, and the latest versions of Chrome and FF 我在IE7-9和最新版本的Chrome和FF中进行了测试

由于您正在使用RichFaces,因此可以简单地使用以下代码:

<a:status id="globalStatus" onstart="onRequestStart()" onstop="onRequestEnd()" />

Using a4j:status should work, but it has to be inside an h:form tag: 使用a4j:status应该可以,但是必须在h:form标记内:

<h:form id="randomForm" styleClass="edit">
        <a:status id="stateStatus"
         onstart="Richfaces.showModalPanel('waitBx'),document.getElementById('randomForm:search').disabled=true;"
         onstop="Richfaces.hideModalPanel('waitBx'),document.getElementById('randomForm:search').disabled=false;"
        styleClass="message" >
</a:status>

...... way more code  
</form> 

After every ajax call this pops up a wait picture and disables the search button. 在每次ajax调用之后,这会弹出等待图片并禁用搜索按钮。

Interestingly enough, at least in our code, this doesn't work for anything in a nested a4j:region. 有趣的是,至少在我们的代码中,这不适用于嵌套的a4j:region中的任何内容。

我认为这就是您要寻找的东西: 在jQuery中使用全局Ajax处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 a4j:ajax 或 a4j:actionListener 从 javascript function 调用 bean 方法? - How to call bean method from javascript function using a4j:ajax or a4j:actionListener? 使用A4J,如何重新呈现javascript函数,并在重新呈现后调用它? - using A4J, how do I rerender a javascript function, and call it after rerendering? 在完成方法bean后执行javascript函数( <a4j:support> ) - Executing javascript function after finishing method bean (<a4j:support>) RichFaces 4- <a4j:ajax …> Javascript“未找到RichFaces” - RichFaces 4 - <a4j:ajax …> Javascript “RichFaces not found” AJAX请求完成后,如何调用PartialView中定义的JavaScript函数? - How can I call a JavaScript function defined in a PartialView after an AJAX request completes? Javascript,如何在循环中每秒同步调用 function? - Javascript, how can I synchronously call function every second in the loop? AJAX加载内容后如何调用javascript函数? - How do I call a javascript function after AJAX loads content? 如何在A4J中访问JavaScript值 - How to access javascript value in a4j 如何在每个ajax或完整请求后调用javascript - How to call javascript after every ajax or full request 在窗口卸载时从javascript调用a4j:commandButton - call a4j:commandButton from javascript on window unload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM