Access to primefaces widgetvars on document ready

I had success with adding the javascript initialization code in the page AFTER the component with the widgetVar, ie

<p:dataTable widgetVar="test">

</p:dataTable>

<script  type="text/javascript">
  $(document).ready(function()  {
     PF('test'); // access and do whatever here
 }
</script> 

Putting the script tag before the p:dataTable doesn't work.

More info here: http://forum.primefaces.org/viewtopic.php?f=3&t=35718


Sometimes the widgetVar needs some time to get initialized (in document.ready), like (non-extensive)

  • upload
  • dialog
  • overlay

in that case you can use setTimeOut

setTimeout(PF('myDialog').show(), 2000);

Hope this helps.


I found a better solution for my case. I call a JavaScript method using the onload attribute of h:body

<h:body onload="checkIfShowDialog()">

And this is the JavaScript method:

function checkIfShowDialog(){
  var showDialog = getUrlParameter("showDialog");
  if (showDialog == "true") {
    PF('myDialog').show();
  }
}

This works as desired.