I need to re-execute javascript just after form gets re-rendered. Simply, putting in javascript after XHTML content won't help since its a partial request. Also, I cannot use "onComplete" as the commandButton from which I am re-rendering form is in JSF component used at several places. I need to get the fix in locally.
Is there any way? I was wondering whether f:ajax would help in this case.
Please let me know, if anyone has clue about it.
Simplest way is to just put the JS function call in the to-be-updated component itself.
This way it's executed as the page loads and also if the form get updated by ajax.
As to the
<f:ajax>
itself, you could also use itsonevent
attribute.with
You could add a global hook by
jsf.ajax.addOnEvent()
so that you don't need to specify it in every singleonevent
attribute of<f:ajax>
if the functional requirement applies to every ajax request.An alternative is to use the OmniFaces JSF utility library which offers the
<h:onloadScript>
for exactly this purpose. You can place it in the head so you want.This is automatically re-excuted on every single ajax request on the view so that you don't need to copy it over multiple individual places in the view which can be ajax-updated, or to repeat its parent ID in every
<f:ajax render>
.You could add jQuery to your application and do something like:
You can take a look here http://api.jquery.com/load/
LE: that being on
ofc.
BalusC first answer is the most appropriate if you want to do complex operations with JQuery/JS and are not particularly concerned about this
For more complex JS, if you decide to use a global event handler and for example you use JS to
bind
functions to elements, these functions are going to be re-bound (hence executed twice), so you need some way to identify what needs to be re-executed.What I finally did, was to get the id's from the event callback and execute only the relevant JS/JQuery:
Note: these ugly
for
loops are probably not required (you could use clildren[0]), but I haven't found the time yet to check the spec.