How to add a JavaScript at a page's end (before the closing </body> HTML tag)?

See how the core does it for the backend pages:


See also: