I understand that you want so show a string value which is been set by the action method? You can only show it once it has been completed. You need to realize that JSF and JS does not run in sync as you'd expect from the coding. JSF is basically a HTML code generator. JS only works on the current HTML DOM tree. You need to let JSF generate HTML code in such way that the JS code get executed once the HTML response arrives at the webbrowser.
Assuming that you have this JS:
function show(string) {
alert(string);
}
And assuming that you have this bean:
private String message;
public void show() {
message = "Hello!";
}
public String getMessage() {
return message;
}
If you're using JSF 1.x, use the following approach:
<h:commandButton value="show" action="#{bean.show}" />
<h:panelGroup rendered="#{not empty bean.message}">
<script>show('#{bean.message}');</script>
</h:panelGroup>
If you're using JSF 2.x which supports ajax, you could alternatively use the following approach:
<h:commandButton value="show" action="#{bean.show}">
<f:ajax render="message" />
</h:commandButton>
<h:panelGroup id="message">
<h:panelGroup rendered="#{not empty bean.message}">
<script>show('#{bean.message}');</script>
</h:panelGroup>
</h:panelGroup>