JavaScript How to know the javascript event source on function scope? javascript know event listeners

Assuming that you can at least pass "timer" in the very first function a:

Fiddle

<a onclick="a()" >call</a>

<script>

function a(){
    b();
}

function b(){
    final();
}

function final(){
    var callerFunction = arguments.callee.caller;
    var evtArg = callerFunction.arguments[0];
    while (callerFunction.caller !== null) {
        callerFunction = callerFunction.caller;
        if (callerFunction.arguments[0]) {
            evtArg = callerFunction.arguments[0];
        }
    }
    console.log(evtArg);
}

setTimeout(function(){a("timer")}, 100);

</script>

This will get you the very last possible first argument in the chain of function calls. So if you use "normal" events it will give you the event object. And if you use a timeout it will give you whatever you pass to the first function.

Note that this solution also uses arguments.callee.caller which is supposed to be slow and not supported everywhere. And as robC already mentioned, it is not allowed in strict mode.

Answer:1

It is possible to throw an exception in the innermost function, catch it, and use the exception to determine the call stack.

The details of how to obtain the call stack are vendor specific (e.stack in FF, e.message in Opera, sketchy function body parsing in IE and Safari), but a reasonably robust implementation exists on eriwen.com.

This, at least, is what I can make out from the short snippet posted on that page. Do note that this has evolved into a full blown project on Github, and as such is likely to be more reliable and feature packed than the 50 line snippet on that page.

In your example, you would use:

function b(){
    final();
}
function final(){
   var trace = printStackTrace();
   //output trace
}


//This would be attached as the click handler for the anchor
function anchorHandler(){
   b();
}
setTimeout(function timerCallback(){
    b();
}, 1000);

Depending on whether timerCallback or anchorHandler are in the trace, you know what event triggered the function call.

Answer:2

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...

How can I determine if a string contains one of the values from an array? For example: var a = ["abc","def","ghi"]; var s = "jskljfdkljflkjk abc jskfdjklsj"; for(var i=0;i<a.length;i++){ if(...

How can I determine if a string contains one of the values from an array? For example: var a = ["abc","def","ghi"]; var s = "jskljfdkljflkjk abc jskfdjklsj"; for(var i=0;i<a.length;i++){ if(...

  1. array values exist php
  2. array_values exist
  3. javascript array values exists

I'm write this code for show get point from asp button and show the point on the map: <script src="http://maps.googleapis.com/maps/api/js"></script> <script> var myCenter; ...

I'm write this code for show get point from asp button and show the point on the map: <script src="http://maps.googleapis.com/maps/api/js"></script> <script> var myCenter; ...

  1. show halfway point google maps
  2. show point on google map
  3. show north point on google maps
  4. show multiple point on google maps
  5. javascript show point on google map
  6. google show point

The code is: var someVariable; // this variable is declared but not initialised... alert(typeof someVariable); // alerts 'undefined' alert(typeof notDeclared); // also alerts 'undefined' ...

The code is: var someVariable; // this variable is declared but not initialised... alert(typeof someVariable); // alerts 'undefined' alert(typeof notDeclared); // also alerts 'undefined' ...

  1. what does ���� mean
  2. what does smh mean
  3. what does boomer mean
  4. what does ok boomer mean
  5. what does the fox say
  6. what does simp mean
  7. what does sos mean
  8. what does dna stand for
  9. what does noel mean
  10. what does asl mean
  11. what does ig mean
  12. what does gap stand for
  13. what does otp mean
  14. what does uwu mean
  15. what does mean
  16. what does goat mean
  17. what does bomboclaat mean
  18. what does pansexual mean
  19. what does woke mean
  20. what does ion mean