Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Programmatically adding multiple functions to one event handler (JS)

Programmatically adding multiple functions to one event handler (JS)

Scheduled Pinned Locked Moved Web Development
javascriptcsharptoolstutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Domenic Denicola
    wrote on last edited by
    #1

    In JavaScript, in my script (i.e. not in the XHTML code), how would I add multiple functions to an event handler? For example, in JS you can do this:

    onload = MyFunction();

    And in C# you can do this:

    event = EventHandler
    event += AnotherEventHandler

    But can you do something similar in JS?


    -Domenic Denicola- [CPUA 0x1337] “I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”

    B 1 Reply Last reply
    0
    • D Domenic Denicola

      In JavaScript, in my script (i.e. not in the XHTML code), how would I add multiple functions to an event handler? For example, in JS you can do this:

      onload = MyFunction();

      And in C# you can do this:

      event = EventHandler
      event += AnotherEventHandler

      But can you do something similar in JS?


      -Domenic Denicola- [CPUA 0x1337] “I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      Perhaps you can create a single set of functions that serve as an event dispatcher and bundle them up as a .js:

      function HookEvent(oElement, cEventName, fFunction) {
      var iIndex = -1;

      if (oElement.EventList == null)
      oElement.EventList = new Array();
      else
      iIndex = FindEventIndex(oElem, cEventName);

      SetEventFunction(oElement, cEventName, iIndex, fFunction);
      }

      function FindEventIndex(oElem, cEventName) {
      var aEvents = oElem.EventList;
      if (aEvents != null)
      {
      for (var iLup = 0; iLup < aEvents.length; iLup++)
      {
      if (aEvents[iLup][0] == cEventName)
      return iLup;
      }
      }

      return -1;
      }

      function SetEventFunction(oElement, cEventName, iIndex, fFunction) {
      if (iIndex > -1)
      {
      var aEventFcns = oElement.EventList[iIndex][1];
      aEventFcns[aEventFcns.length] = fFunction;
      }
      else
      {
      oElement.EventList[oElement.EventList.length] = new Array(cEventName, new Array(fFunction))
      }

      eval("oElement." + cEventName + " = ProcessEventHandlers");
      }

      function ProcessEventHandlers() {
      var oElem = window.event.srcElement;
      var cEvent = "on" + window.event.type;

      var iIndex = FindEventIndex(oElem, cEvent);
      if (iIndex > -1)
      {
      var aFcns = oElem.EventList[iIndex][1];
      if (aFcns == null)
      return;

      for (var iLup = 0; iLup < aFcns.length; iLup++)
        {
          eval(aFcns\[iLup\]);
        }
      

      }
      }

      NOTE: THis is just something i typed in and is not guaranteed to work. Just for example purposes.  onwards and upwards...

      B 1 Reply Last reply
      0
      • B basementman

        Perhaps you can create a single set of functions that serve as an event dispatcher and bundle them up as a .js:

        function HookEvent(oElement, cEventName, fFunction) {
        var iIndex = -1;

        if (oElement.EventList == null)
        oElement.EventList = new Array();
        else
        iIndex = FindEventIndex(oElem, cEventName);

        SetEventFunction(oElement, cEventName, iIndex, fFunction);
        }

        function FindEventIndex(oElem, cEventName) {
        var aEvents = oElem.EventList;
        if (aEvents != null)
        {
        for (var iLup = 0; iLup < aEvents.length; iLup++)
        {
        if (aEvents[iLup][0] == cEventName)
        return iLup;
        }
        }

        return -1;
        }

        function SetEventFunction(oElement, cEventName, iIndex, fFunction) {
        if (iIndex > -1)
        {
        var aEventFcns = oElement.EventList[iIndex][1];
        aEventFcns[aEventFcns.length] = fFunction;
        }
        else
        {
        oElement.EventList[oElement.EventList.length] = new Array(cEventName, new Array(fFunction))
        }

        eval("oElement." + cEventName + " = ProcessEventHandlers");
        }

        function ProcessEventHandlers() {
        var oElem = window.event.srcElement;
        var cEvent = "on" + window.event.type;

        var iIndex = FindEventIndex(oElem, cEvent);
        if (iIndex > -1)
        {
        var aFcns = oElem.EventList[iIndex][1];
        if (aFcns == null)
        return;

        for (var iLup = 0; iLup < aFcns.length; iLup++)
          {
            eval(aFcns\[iLup\]);
          }
        

        }
        }

        NOTE: THis is just something i typed in and is not guaranteed to work. Just for example purposes.  onwards and upwards...

        B Offline
        B Offline
        Bjoern Graf
        wrote on last edited by
        #3

        First, don't use eval. Never ever as it'll recompile the current script plus the stuff inside the eval :) To attach more than one event handler to an element, use the method attachEvent (IE) or for w3c browsers addEventListener[^]: var el = document.getElementById("foo"); el.attachEvent("onclick", foo); el.attachEvent("onclick", function () { alert("2nd handler"); }); function foo(event) { alert("1st handler"); }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups