How to call a JS function by passing parameter as soon as the page is loaded?

Asked

Viewed 1,075 times

0

I have to call a function in my JS by passing as parameter an array, when the page is loaded.

I can do it using one p:commandButton, where here: actionListener="#{rotaEntregaBean.gerarMapa()}" i run the function in the Bean (taking values in the database) and then, aqui:oncomplete="initMap(xhr, status, args)" i run the JS function.

Knob

<p:commandButton id="btnMapa" value="Gerar Rota" icon="ui-icon-pin-s" 
 actionListener="#{rotaEntregaBean.gerarMapa()}"
 style="float:right" oncomplete="initMap(xhr, status, args)"/> 

Bean

Bean

But I’m willing to do it without having to click the button, once I load the page.
In my page I already start a Bean method as shown in the image below, but I don’t know how to call the JS function with parameter.

Calling Bean method as soon as the page is loaded:

<f:metadata>  
    <o:viewParam name="entrega_id" value="#{rotaEntregaBean.id_entrega}" />
    <f:viewAction action="#{rotaEntregaBean.inicializar}" />
</f:metadata>

JS function

function initMap(xhr, status, args) {   
    var qtd_entregas = args.coord.length;

    for (var i = 0; i < args.coord.length; i++) {
        waypts.push({
            location : args.coord[i].latitude + ', ' + args.coord[i].longitude,
            stopover : true,
        });
    }
...
}

1 answer

0

You can run the JS inside your bean. Call the function as soon as you finish filling your array. To run inside the bean is like this:

RequestContext.getCurrentInstance().execute("testeJS();");
  • I tried this way, but as I will pass the vector by parameter to the JS function I am calling?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.