views:

140

answers:

4

If you don't control the invocation of your code, but would like to know if it's being executed by an event handler, is this possible?

A: 

You could have it signal by modifying a global variable. For debugging purposes, there's an alert.

sblundy
+3  A: 

Javascript has event object that can help you identify the source of the event.

shahkalpesh
Of course, for all browsers other than IE, you have to be the event handler itself to get access to it.
bobince
A: 

Um...

There's really only 3 places to put javascript (that I know of): 1) inside of script tags directly, 2) inside of a function inside of a script tag, and 3) in a link or event directly (like... onClick="", etc.).

In the case of (1) it executes as soon as loaded by the browser and in this case it wouldn't really be invocated by an event. But, in all other cases, the only way to get the code to execute is because of an event.

So, it seems to me that it is pretty easy to know if your code was executed because of an event...

Now, if you want to know the specific event handler that is executing your code, that is another matter. :D

BoltBait
A: 

You could maybe do this if you had a way to look up the stack, but that's not a very good way to do it. This article shows how to access the stack.

A Javascript stacktrace in any browser

sjbotha