The error occurs when the window of the parent script is located (i.e. closed), but refers to a script that is still held (for example, in another window). Although the “object” is still alive, the context in which it wants to execute is not.
It is somewhat dirty, but it works for my Windows sidebar gadget:
Here is a general idea: In the "main" window, a function is created that will define some code, yes, this is ugly. Then the “child” can call this “builder function” (which is bound to the main window area /) and returns a function that is also bound to the “main” window. The obvious drawback is, of course, that the function, the “bounce”, cannot be closed over the area in which it is apparently defined ... in any case, flexibility is enough:
This is partly pseudo-code, but I use it in the Windows sidebar gadget (I keep talking about this because the sidebar gadgets work in "unlimited zone 0", which may or may not change the script to a large extent.)
// This has to be setup from the main window, not a child/etc! mainWindow.functionBuilder = function (func, args) { // trim the name, if any var funcStr = ("" + func).replace(/^function\s+[^\s(]+\s*\(/, "function (") try { var rebuilt eval("rebuilt = (" + funcStr + ")") return rebuilt(args) } catch (e) { alert("oops! " + e.message) } } // then in the child, as an example // as stated above, even though function (args) looks like it // a closure in the child scope, IT IS NOT. There you go :) var x = {blerg: 2} functionInMainWindowContenxt = mainWindow.functionBuilder(function (args) { // in here args is in the bound scope -- have at the child objects! :-/ function fn (blah) { return blah * args.blerg } return fn }, x) x.blerg = 7 functionInMainWindowContext(6) // -> 42 if I did my math right
Alternatively, the main window should be able to pass the functionBuilder function to the child window - provided that the functionBuilder function is defined in the context of the main window!
It seems to me that I used too many words. YMMV.
user166390 Sep 01 '09 at 5:35 2009-09-01 05:35
source share