eriwen / stacktrace.js

<script type="text/javascript" src="path/to/stacktrace.js" />  
<script type="text/javascript">      
... your code ...      
if (errorCondition) {
          var trace = printStackTrace();
          //Output however you want!           
		   alert(trace.join('\n\n'));      
}      
... more code of yours ...  
</script>

Tested in No-options test

<script type="text/javascript">
      var lastError;
      try {
          // error producing code
      } catch(e) {
         lastError = e;
         // do something else with error
      }
        // Returns stacktrace from lastError!
      printStackTrace({e: lastError});
</script>

Tested in passing error test

window.onerror = function(msg, file, line) {
      alert(printStackTrace().join('\n\n'));
	    }

Tested in window.onerror test

var p = new printStackTrace.implementation();
  p.instrumentFunction(this, 'bar', logStackTrace);  
function logStackTrace(stack) {
      console.log(stack.join('\n'));
  }  function foo() {
      var a = 1;
      bar();
  }  
function bar() {
      baz();
  }  
foo(); //Will log a stacktrace when 'bar()' is called containing 'foo()'!
    p.deinstrumentFunction(this, 'bar'); //Remove function instrumentation

Tested in function instrumentation test