Mai 29 2007
Event driven programming with jQuery
These days, I have written a software, which extensively uses JavaScript. In this way, I wanted to call functions after the user has done specific actions. How can I do this?
The old way:
After each action, I have to write my functions, which should be called. This method works, of course. But it is not clear. It is more interesting to define my functions and than I want to define, after which actions these functions should be called. That should be done without changing the action. So I have tried to use jQuery for this way.
The new way:
jQuery offers the ability to abstract event handling between browsers. And this abstraction is done very well. It allows to expand possible events.
Imagine, you have an action called “action1″. I want to register some functions, which should be called in this action. So try this:
$(document).bind(“action1Call”,function1);
$(document).bind(“action1Call”,function2);
It’s easy to call function1 and function2 in action1 now: Just write…
$(document).trigger(“action1Call”);
… to the end of your action. That’s it. You can add new functions easily by binding them. Even no binding is possible (if there is no function, which should be called).


[...] Mathias Bank, der Entwickler des jQuery-Plugins getUrlParam, beschreibt unter dem Titel “Eevent driven programming with jQuery eine interessante Möglichkeit, mit jQuery Funktionen dynamisch an Ereignisse zu binden. [...]