Reversing the preceding recipe, takes the form of passing variables from JSF to JS (from server to client).
We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.
The solution is straightforward, and it is presented next (we have passed a constant and an EL result to a JS function, using the onload
event with the h:body
task:
<script type="text/javascript" language="javascript"> function variableFromServer(variable){ alert("Variable from server: " + variable) } </script> … <!-- passing a constant --> <h:body onload="variableFromServer('JSF_1'),"> <!-- passing the EL result --> <h:body onload="variableFromServer('#{bean.text}'),">
We passed a variable using the onload
event, but this is just an example. You can follow this example to use any other events and conjunctures, accordingly to your application's needs.