Here's a quick example, which pops up a dialog box when the page has finished loading.
You can also run a function when the user leaves a Web page with the onUnload handler - as the following example demonstrates:
confirm("Are you sure you want to leave this Web page?");
onLoad and onUnload can be used individually, or together.
Yes, it is. Though only so long as you have a couple of lines of code to be executed - anything more complicated, and you're better off putting it into a function.