Is it possible to copy a function (or any object for that matter) from one window context to another in Javascript?
Let's say I have a parent window with a frame in it. The frame defines a function foo()
, which I'd like to copy to the outer window. (One reason to do it would be so that foo()
is available when the frame document navigates to a different URL.)
I know I can easily create a reference to foo()
in the parent by doing
function foo() { ... }
parent.foo = foo;
But with this method foo()
still lives in the frame document, and will not be available to the parent should frame get unloaded.
I know I can also create a new function in the parent by using the Function constructor:
parent.foo = new parent.Function(" ... ");
However, this requires me to have my function as a JS string.
DOM supports moving nodes from one document to another via importNode()
. Does Javascript have a similar feature?