title: JQuery event model and preventing duplicate handlers
body: <p>Once again I want to load a page which contains its own script into a div using $("divid").load(...).  The problem I face is related to events.  Let's say we trigger("monkey") from the parent page and on the loaded page we bind("monkey") and just do an alert("monkey bound").  If the same load method is called multiple times, the bind is called multiple times.  Now I could just unbind it before I bind it, or check the number of handlers before the bind and then not bind it to prevent this.  Neither option is scalable as what if I later want to bind to that trigger in another "sub page" (a page loaded into a div).</p>
  
  <p>What I ideally want to do then is check if the handler I am about to add already exists, but I still WANT to use anonymous handlers... (asking a bit much with that last request I think).  Currently I have a workaround by using pre-defined/named methods and then checking this before the bind.</p>
  
  <pre><code>// Found this on StackOverflow
  function getFunctionName(fn)
  {
   var rgx = /^function\s+([^\(\s]+)/
   var matches = rgx.exec(fn.toString());
   return matches ? matches[1] : "(anonymous)"
  }
  
  function HandlerExists(triggerName, handlerName) {
          exists = false;
          if ($(document).data('events') !== undefined) {
              var event = $(document).data('events')[triggerName];
              if(event !== undefined)
              {
                  $.each(event, function(i, handler) {
                      alert(handlerName);
                      if (getFunctionName(handler) == handlerName) {
                          exists = true;
                      }
                  });
              }
          }
          return exists;
      }
  </code></pre>
  
  <p>This is a pretty crude way of going about it I feel, but appears to work. I just do the following before the bind as follows:</p>
  
  <pre><code>if (!HandlerExists("test", "theMethod")) {
      $(document).bind("test", theMethod);
  }
  </code></pre>
  
  <p>Does anyone have a more elegant solution? for instance, is there any way to check a particular script is loaded? so I could use getScript() to load the js from the child page on first load, and then simply not load it on subsequent loads (and just fire a trigger which would be handled by he preexisting js)..</p>