views:

762

answers:

2

I came across this method to produce a Javascript stack trace (to fix an IE specific bug): http://pastie.org/253058.txt which sounds really useful, but when I call it, the stack trace I get is for the code of the script itself?!

Probably a simple for someone who knows javascipt, but I don't!

Or if you can suggest a better way to get a stack trace in IE, please let me know.

A: 

You might be better off using IE 8's built-in debugger.

Tim Down
Thanks for your suggestion. Unfortunately, i'm stuck with IE7 (corporate network and all that).
Gavin
Even with IE7 you can turn javascript debugging on in internet options and launch visual studio on error.
Mike Blandford
Thanks for your suggestion Mike. Unfortunately, due to the same network restrictions I can't enable the Javascript debugger in IE.
Gavin
+4  A: 

This getStackTrace() function creates the stack trace of the function from which you've called getStackTrace(). It does not create the stack trace of an error that you've caught. For example, you'd use it to try to figure out how a specific function is being called:

function foo() {
    // debug how this is being called
    alert(YOUR_NAMESPACE.getStackTrace());
}

Or to add some more detail to an error you raise:

function foo() {
    // signal something went wrong
    var error = new Error("error in foo");
    if (!error.stack)
        error.stack = YOUR_NAMESPACE.getStackTrace();
    throw error;
}

You can not use it like this:

try {
    foo();
} catch (e) {
    alert(YOUR_NAMESPACE.getStackTrace(e));
}

Here's a good rundown of what stack information you can get -- and from which browsers -- when an error occurs: Three Painful Ways to Obtain a Stack Trace in Javascript

Justin Ludwig
Thanks Justin, this is what I was looking for. The problem appears to be the way I was calling it. Thanks a lot.
Gavin