Listening and firing events with Javascript and maybe jQuery
Posted
by
at
on Stack Overflow
See other posts from Stack Overflow
or by at
Published on 2011-01-07T00:44:31Z
Indexed on
2011/01/07
0:54 UTC
Read the original article
Hit count: 470
In my Javascript and Flex applications, users often perform actions that I want other Javascript code on the page to listen for. For example, if someone adds a friend. I want my Javascript app to then call something like triggerEvent("addedFriend", name);
. Then any other code that was listening for the "addedFriend" event will get called along with the name.
Is there a built-in Javascript mechanism for handling events? I'm ok with using jQuery for this too and I know jQuery makes extensive use of events. But with jQuery, it seems that its event mechanism is all based around elements. As I understand, you have to tie a custom event to an element. I guess I can do that to a dummy element, but my need has nothing to do with DOM elements on a webpage.
Should I just implement this event mechanism myself?
© Stack Overflow or respective owner