How to execute JavaScript after page load? How to execute JavaScript after page load? javascript javascript

How to execute JavaScript after page load?


Use JQuery as follows:

<script>    jQuery(document).ready(function() {        PathFinder.findAndGo();    });</script>

Update:

It has to be within <ui:define name="content">.


There are 2 ways that work for me, when I want to execute a JS function after page load in Primefaces:

  • either use jQuery (as it is already used by Primefaces), best solution is to nest document.ready twice, so that my JS code is executed after all Primefaces code:
    • jQuery(document).ready(function () { jQuery(document).ready(function () { // twice in document.ready to execute after Primefaces callbacks PathFinder.findAndGo(); });});
  • or use p:remoteCommand with autorun, and place oncomplete JS callback on it
    • this way form is submitted by AJAX to server but no listener executed on server side, a JS callback is executed afterwards
    • <p:remoteCommand oncomplete=" PathFinder.findAndGo(); " autoRun="true"/>


window.onload = function () {  // code to execute here}