I want to load JavaScript function on startup with window.onload and that function would pass its parameters to a Managed Bean.
Without a commandButton.
JS:
window.onload = function getVars() {
// ...
var x = locationInfo.lng;
var y = locationInfo.lat;
document.getElementById("formId:x").value = x;
document.getElementById("formId:y").value = y;
}
XHTML:
<h:form id="formId">
<h:inputHidden id="x" value="#{bean.x}" />
<h:inputHidden id="y" value="#{bean.y}" />
</h:form>
Managed Bean is a regular managed bean with its getters and setters.
This code is not working without clicking a commandButton.
Is there way to load parameters with JavaScript and pass it to JSF?
You can do it with remoteCommand.
<p:remoteCommand name="sendArgs" actionListener="#{mybean.rcvArgs()}" partialSubmit="true" process="@this"/>
and do a javascript call
sendArgs([
{name: 'xval', value: locationInfo.lng},
{name: 'yval', value: locationInfo.lat}
]);
in mybean you have to take the arguments from the parameter list
public void rcvArgs() {
Map<String, String> pmap;
pmap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String xval = pmap.get("xval");
String yval = pmap.get("yval");