Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Brent Fitzgerald http://brentfitzgerald.com/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

javascript window event handling manager

I've seen a lot of window.onload managers, so I generalized it for any window event handler. This assumes prototype.
   1  
   2  // BurntoEventManager
   3  // http://brentfitzgerald.com/
   4  // January 2007
   5  
   6  var BurntoEventManager = {
   7      handlers: {},
   8      add: function(handler_name, method) {
   9          if(this.handlers[handler_name] == null) {
  10              this.handlers[handler_name] = new Array();
  11          }
  12          this.handlers[handler_name].push(method);
  13          
  14          // Now update the window event handler
  15          window[handler_name] = function(evt) {
  16              this.handlers[handler_name].each(function(m) {
  17                  m(evt);
  18              }.bind(this));
  19          }.bind(this);
  20      },
  21      
  22      clear: function(handler_name) {
  23          this.handlers[handler_name] = null;
  24          window[handler_name] = function() {};
  25      },
  26      
  27      get: function(handler_name) {
  28          return this.handlers[handler_name];
  29      }
  30  }

For example, consider if later on in our application we want to add an onclick handler.
   1  
   2  BurntoEventManager.add("onclick", function(evt) { alert(evt) });
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS