Javascript execution * after * ajax update

I need to execute a specific javascript function after the ajax update has been applied. I tried to jsf.ajax.addOnEvent(ch.ergon.newom.ajaxUpdate) find out about the request, but it seems to execute after receiving the ajax response, but before it was applied (that is: the content was updated).

How to execute JavaScript function after updating JSF-ajax content?

+1
source share
1 answer

Actually, it will be called three times. One before sending the ajax request, one after the ajax response, and one when the ajax response is successfully processed. You should check the current status based on the data argument. You can find technical details in tables 14-4 and 14-3 of the JSF 2.0 specification .

Here is an example of launching what your JS function should look like when you want to connect all 3 states.

function ajaxUpdate(data) {
    var ajaxStatus = data.status; // Can be 'begin', 'complete' and 'success'.

    switch (ajaxStatus) {
        case 'begin': // This is called right before ajax request is been sent.
            // ...
            break;

        case 'complete': // This is called right after ajax response is received.
            // ...
            break;

        case 'success': // This is called when ajax response is successfully processed.
            // ...
            break;
    }
}

Or if you want to connect only to the status success:

function ajaxUpdate(data) {
    if (data.status == 'success') {
        // ...
    }
}
+2
source

All Articles