How to run JavaScript on page load without `<body onload>`, especially in JSP fragments without a `<body>` tag?

If you can control where the script tag is placed, put your JavaScript just before the closing </body> tag:

<script>
  foo();
</script>

Since the DOM will already be loaded by then, your function runs immediately without extra event listeners.

Hope this was helpful :slight_smile: