Pages

Friday, June 29, 2012

stack trace javascript

stack trace in javascript
This was requirement in my current project, where i need to log real JavaScript error. Most of the time i was getting my javascript error was giving line numbers of jquery main javascript files but error was somewhere else so I decided to trace js error.


this is simple code you can try to trace. you can write green block of code wherever you want to trace.
you can also write a plugin or a common function and call where ever you want.

this is simple demo of understanding tracing error. 

I found a gud link for arguments object. this is very simple page which will make u understand arguments object.

http://www.seifi.org/javascript/javascript-arguments.html


<script>
function funcallee ()
{
    var trace ="", currentFunction = "";             
    try{
         currentFunction = arguments.callee.caller;                                       
           while (currentFunction) {
                 var fn = currentFunction.toString();                 
                 var fname = fn.substring(fn.indexOf("function"), fn.indexOf(")") +1 ) || 'n/a';                                       
                trace = trace + ' --> '+ fname;                    
                 currentFunction = currentFunction.caller;
           }
    }catch (e){
        trace= "";
    }     
    alert (trace);       
}             
             
function testcaller_1()
{
    try{
    var h = j;
    alert (h);
    }
    catch(e){
        funcallee();
    }
}          

function testcaller_2()
{
    testcaller_1();   
}
   
testcaller_2();


</script>

No comments:

Post a Comment