Permalink
Browse files

added document.onReady and document.onLive

  • Loading branch information...
juzna committed Jan 13, 2011
1 parent 24bd9d6 commit cb3695676ed4bc8bc8c6f14fdba756efa5d2a2fb
Showing with 47 additions and 0 deletions.
  1. +47 −0 src/prototype/dom/dom.js
View
@@ -3675,3 +3675,50 @@ Element.addMethods({
return (el && el.tagName == 'HTML');
}
});
+
+
+// On-ready callbacks on document
+(function() {
+ // List of live handlers
+ var onLiveHandlers = [];
+
+ // Execute all registered live handlers
+ function executeLiveHandlers(container) {
+ onLiveHandlers.each(function(item) {
+ item.callback(container)
+ })
+ }
+
+ // Add new live handler
+ function addLiveHandler(name, cb) {
+ if(Object.isFunction(name)) {
+ cb = name;
+ name = 'anonymous-' + document.onLiveHandlers.length;
+ }
+
+ // Add on-live handler
+ onLiveHandlers.push({
+ name: name,
+ callback: cb
+ });
+
+ return name;
+ }
+
+ // Execute callback when document get's ready
+ function onReady(cb) {
+ if(document.ready) cb.defer();
+ else Element.observe(document, 'load', cb);
+ }
+
+ // Wait for document to be ready
+ Element.observe(document, 'load', function() {
+ document.ready = true;
+ executeLiveHandlers(document.body);
+ });
+
+
+ // Export methods
+ document.onReady = onReady;
+ document.onLive = addLiveHandler;
+})();

0 comments on commit cb36956

Please sign in to comment.