While there is no reason it wouldn't work using the onload attribute of the <body> element, you could also consider using a twig {% js %} tag.
The {% js %} tag allows you to register JS to be added in your page from anywhere in your template.
By default it will be added at the end of the <body> element, which guarantees your HTML document is loaded when the JS is executed.
Exemple:
{% js %}
console.log('document loaded');
{% endjs %}
The main drawback of this approach is that the JS will be part of your HTML page source so it might be repeated on several pages depending on where you call it in your template.
There are many other (better) ways to do this but they're not Craft specific.