1

We want to log the JavaScript on our Website. For that I am overwriting the log functionality with my own implementation.

On the server side we are using Log4Net for our logging. My idea now is to capture the log on the client side and send it to the server with ajax. How ever I don't want to send the Ajax request every time someone types log. So my idea was to catch all the logs and send ONE request after the call-stack ended up where it has began.

Is there an event or a way to detect when call-stack has "ended" ?

Kevin Boss
  • 145
  • 12

1 Answers1

1

You can try to use javascript beforeunload event to send your log when user leave a page. If you use jQuery, the code look like:

$(window).bind("beforeunload",function(event) {
    //... put your ajax code here ...

    return "The log has sent to server.";
});

however beforeunload event will show a message box on your screen.

Also if you want to run a function after another function run, you can use jQuery $.when() method as below:

$.when(functionOne(), functionTwo()).then(functionThree()).done(functionFour());

functionOne(), functionTwo(), functionThree(), and functionFour() must be a deferred object. This blog give a simple explanation how to implement deferred object.

Habibillah
  • 27,347
  • 5
  • 36
  • 56
  • This event triggers when the user leaves the site. I want to have an event which calls when a function ends. Like this `function one(){ } function two(){ one(); } two(); ` //At the end of two, the event should trigger as it is up the call-stack. – Kevin Boss Jun 07 '13 at 06:18
  • 2
    @KevinBoss why not just add a function to the end after two() that calls the log function? –  Jun 07 '13 at 06:31
  • Because I don't want the user of the function to handle the ajax. – Kevin Boss Jun 07 '13 at 06:36
  • I will work with a timeout instead and reset the timeout when new logs will be added. Unload seems to be to dangerous for loosing the logs. – Kevin Boss Jun 07 '13 at 14:07