Full table for multiple JS frames on IE8

I need to get a full call stack if an exception occurs in JavaScript in Internet Explorer 8. Function calls are possible between frames, the number of which is large.

The call stack needed to send logs to developers. I cannot use the debugger because the end user should not deal with this problem.

The current solution for JavaScripts, provided it can generate callstack ( http://eriwen.com/javascript/js-stack-trace/ ). It is based on arguments.callee.caller. But the caller returns zero (undefined) if the function is called from outside the current frame. Thus, the resulting column is incomplete.

Can I get the name of the frame from which the function was called in this case?

The solution based on Active Scripts technology provides an object of type ScriptEngine: IHTMLDocument :: get_Script (IDispatch ** p)

But the "script" cast object on the IActiveScript interface fails.

* Can I get a link from IE8 that will be used for the specified ScriptEngine context to extract the necessary information for building the stop code?

+8
javascript internet-explorer exception-handling javascript-engine
source share
1 answer

I found some way that might be useful. It uses the idea of ​​callbacks.

Define the following simple function in each frame:

function getCaller() { return arguments.callee.caller; } 

and the following functions for the top frame only:

 function populateStack(fn) { var perFrames = []; for (var i = 0; i < windows.length; i++) { var win = windows[i]; var func = (win == this) ? fn : win.getCaller(); var localStack = []; while (func) { localStack.push(getFuncName(func)); func = func.caller; } perFrames.push(getWinName(win) + ": " + localStack.join(", ")); } alert(perFrames.join("\n")); } function getWinName(win) { var m = win.location.toString().match(/^.*\/(.*)$/); return m[1]; } function getFuncName(func) { var m = func.toString().match(/^function\s*(\w*)\(/); return m[1] || "anonymous"; } 

windows should be an array in the top frame containing all window objects (i.e. frames). Using:

 window.top.populateStack.call(window, arguments.callee); 

I spent a couple of hours trying to restore the exact order in which the functions were called, but could not find a solution. Only partial order is available in this code (functions are correctly sorted within frames).

If you have several servers with different versions of the code, you can add code that will analyze the function bodies and through this get additional information about the call order.

Hope this helps :-)

+2
source share

Source: https://habr.com/ru/post/650491/


All Articles