RobG said:
Since head and body elements are mandatory in HTML documents, it is
likely that every document will be created with head and body elements
right from the start. However, that assumption is also likely to fail
in isolated cases since strictly the DOM shoudn't be presumed to be
ready for scripting until the load event occurs.
The window, document, document.documentElement properties are
immediately available for any a script that exists in a valid "normal"
page. The head element is also available, because the script is a
descendant of the head, the body, or a descendant of the body.
A script that expects document.body being the body element when it is
executed (global execution) places a constraint on that script: It must
be included in the body. Accessing document.body immediately can be
desirable in a feature test. The script that wants to access
document.body is limited to a few options.
1) include the script in the body
2) script uses onload to access document.body
3) script uses a "ready()" function to access body
4) script uses another node (not document.body)
5) script provides a function to access document.body later
Lets consider each option.
1) Pro:
Not including the scripts in the head also allows the script access to
document.body, without waiting for onload or "ready()" function.
<aside>
Aside from being able to access document.body, there is one other
consideration: Page load time and load order.
Many browsers will stop rendering content, won't download images or
other scripts until that script has loaded and executed.
If that script is moved all the way down, before the closing body tag,
the browser (any) will parse and render the body and continue to
download images without waiting for that script to load. The page will
load and render faster than if that script had been included in the head.
</aside>
con: Imposing the requirement of not including the script in the head
might not be a troublesome constraint for implementation code. However,
requiring low level code to exist in the body can cause inconvenience
when higher level code wants to use that low-level code, but that higher
level code wants to exist in the head (maybe to include a stylesheet but
avoid the visual effect of re-rendering (which can also impact
performance)). Requiring the low level code to have access to
document.body implies requiring the low level code to exist inside the
body, or requiring that that low-level code use onload or ready. That
means the high level code that depends on that file can't exist in the
head unless it also uses onload or the same "ready()" strategy that the
head used. That's a problem.
2) pro: simple and works in all browsers.
con: onload is slow, particularly if something is hanging.
3) pro: a ready function allows access to the document prior to onload.
con: A ready function, such as found on Dean Edwards site or in jQuery
relies on various nonstandard approaches. The reliable fallback to ready
is onload. If a ready function is used, it must be the same ready
function and that ready function therefore be included in the head.
4) use another node (not document.body)
This can work in some situations, but might be unsafe in other cases.
For example, a feature test that expects the browser to function in a
way that contradicts public w3c standards (such as by appending
elements) would be unsafe.
5) Low-level code provides a function that accesses document.body.
Con: requires redesigning the code in a way that might be more complex.
Con: The higher-level code does not have access the low-level method of
the body immediately. Must wait for onload or "ready()".
I can't think of a good reason to use an onload attribute in the body
tag, except maybe to match someone else's convention.
Why have some event handlers in html and some in the page-js file?
Consistency of behavior? No, that couldn't be it. Consistent
organization of code? No, that isn't the case. Somebody prefers it that
way out of convention? Possibly, but I would question that convention.
Garrett