observe
1.6.0
observe(eventName, handler) -> document
Listens for the given event over the entire document. Can also be used for listening to "dom:loaded"
event.
document.observe
is the document-wide version of Element#observe
. Using document.observe
is equivalent to Event.observe(document, eventName, handler)
.
The "dom:loaded"
event
One really useful event generated by Prototype that you might want to observe on the document is "dom:loaded"
. On supporting browsers it fires on DOMContentLoaded
and on unsupporting browsers it simulates it using smart workarounds. If you used window.onload
before you might want to switch to dom:loaded
because it will fire immediately after the HTML document is fully loaded, but before images on the page are fully loaded. The load
event on window
only fires after all page images are loaded, making it unsuitable for some initialization purposes like hiding page elements (so they can be shown later).
Example
document.observe("dom:loaded", function() {
// initially hide all containers for tab content
$$('div.tabcontent').invoke('hide');
});